home *** CD-ROM | disk | FTP | other *** search
-
- #include "lharc.h"
-
- #define VERSION "2.22"
- #define __030 0
- #define BETA 0
- #define GERMAN 1
-
- #ifdef __SHELL__
- #undef GERMAN
- #undef BETA
- #define GERMAN 0
- #define BETA 0
- #endif
-
- #if GERMAN
- #include "usageger.h"
- #else
- #include "usageeng.h"
- #endif
-
- uchar *errmes[]={
- M_UNKNOWNERR,M_INVCMDERR,M_MANYPATERR,M_NOARCNMERR,M_NOFNERR,M_NOARCERR,M_RENAMEERR,M_MKTMPERR,
- M_DUPFNERR,M_TOOMANYERR,M_TOOLONGERR,M_NOFILEERR,M_MKFILEERR,M_RDERR,M_WTERR,M_MEMOVRERR,M_INVSWERR,
- M_CTRLBRK,M_NOMATCHERR,M_COPYERR,M_NOTLZH,M_OVERWT,M_MKDIR,M_MKDIRERR,M_CRCERR,M_RDONLY};
-
- ushort left[2*NC-1],right[2*NC-1];
- uchar c_len[NC],pt_len[NPT],*len;
- ushort c_freq[2*NC-1],c_code[NC],p_freq[2*NP-1],pt_table[256],pt_code[NPT],t_freq[2*NT-1];
- ushort *freq,len_cnt[17];
- int heap[NC+1];
-
- _DTA _dta;
- XATTR xattr;
- FILE *file1,*file2,*file3,*StdOut=stdout;
- int args,c_err,skipped,cmd,cmdupdate,cmdlist,errorlevel,oldtos;
- int _gemdos,Nfile,fbfiles,cpu;
- long arcpos0,arcpos1,lastarcpos,lastarclen,nextarcpos,arclen,maxlen,old_afx;
- long act_len,file_len,bsize,o_handle=-1,i_handle=-1,o_dev=-1,i_dev=-1;
- uchar act_dir[MAXPATH],basedir[MAXPATH],workdir[MAXPATH],incldir[MAXPATH];
- uchar *unpack="*.ZOO,*.ZIP,*.AR[CJ],*.LZ[HS],*.LHA,*.TAZ";
- uchar *infname,*outfname,*fbuf,*fbnxt,*o_dir;
- ulong fblft,fbmax;
- filebuf *fblast;
-
- uchar SystemId,has_crc,back_1,back_2;
- int patno,exno,maxblk=64,UnixArc,UnixFile,regcnt,wild_arc,multi_arc,all;
- int fn_name=12,pt_name=128,Case=_PC_CASECONV,min_len,garbage,Device;
-
- int n,heapsize,hdr_len,header_len,ignfile;
- uchar *buf=text_buf,*pager,*com_name,*buffer_start,*buffer_last,pack;
- uchar flg_r,flg_p,flg_x,flg_m,flg_a,flg_c,flg_v,flg_w,flg_z,flg_g,flg_L=2,flg_N,flg_B,flg_W,flg_5;
- uchar flg_d,flg_u,flg_s,flg_e,flg_unpacked,ex_len,flg_j,flg_chk,flg_S,flg_R,flg_I,flg_U=1,flg_K;
- uchar flg_t,flg_arc,flg_h,flg_i,flg_backup,flg_f,flg_4,flg_q,flg_A,flg_X,pnt='.',buffered;
- uchar method=5,FlgMethod=5,copying,obj,ptitel,pargs;
- char flg_k=-1;
-
- uchar swi[]="UwCrpxmacntvhifzedgjqsLSRBAWXKybluok54PMNI";
- uchar *swipos[]={&flg_U,&flg_w,&flg_chk,&flg_r,&flg_p,&flg_x,&flg_m,&flg_a,&flg_c,&flg_n,&flg_t,&flg_v,&flg_h,&flg_i,
- &flg_f,&flg_z,&flg_e,&flg_d,&flg_g,&flg_j,&flg_q,&flg_s,&flg_L,&flg_S,&flg_R,&flg_B,&flg_A,&flg_W,&flg_X,&flg_K};
-
- #define SWI_CNT 30
-
- LzHead Hdr1,Hdr2;
- _DOSTIME arcstamp,newer;
-
- uchar arcname[MAXPATH],pathname[MAXPATH],backup1[MAXPATH],backup2[MAXPATH];
- uchar filename[MAXPATH],dosfilename[MAXPATH],matchfilename[MAXPATH],comment[MAXCOMMENT+256];
- uchar buffer[(BUFFERSIZ+128+32)<<2],*buffer_1,*buffer_2,*buffer_3,*buffer_gen;
- uchar travel_wild[MAX_PAT],*exclude_file[MAX_EXCLD],fileregbuf[FILEBUFSIZ],*fileptr;
- uchar *travel_path[MAX_PAT],travel_rel[MAX_PAT];
- int patcnt[MAX_PAT],travel_len[MAX_PAT],travel_file[MAX_PAT],arc_file[MAX_ARC];
-
- #if BETA
- long timer;
- #define INIT_TIMER timer=clock()
- #define EXIT_TIMER timer=clock() - timer; if (!flg_q) printf(" Time : %ld ms \n",(timer*5))
- #else
- #define INIT_TIMER
- #define EXIT_TIMER
- #endif
-
- clock_t now;
-
- void getnow(void)
- {
- now=*((unsigned long *) 0x4baL);
- }clock_t clock(void){ (void) Supexec(getnow); return (now);}
-
- #define A 16807L
- #define M 2147483647L
- #define Q 127773L
- #define R 2836L
- long _lseed;int temp(void){ _lseed=A * ((_lseed % Q) + R) - R * (_lseed / Q); if (_lseed<0) _lseed+=M; return((int) _lseed & 4095);}
- #define multi_wild(f) (strchr((f),',')!=NULL)
-
- int wildcard(uchar *file)
- {
- if (!flg_W && strpbrk(file,"*?[]@|^"))
- return(SUCCS);
- else
- return(FAULT);
- }
-
- void InitTree(void)
- {
- register int i,nil_2=NIL<<1,*p;
-
- p=dad;
- for (i=N;--i>=0;)
- *p++=nil_2;
-
- p=&rson[N+1];
- for (i=256;--i>=0;)
- *p++=nil_2;
- }
-
- int LSeek(FILE *fp, long offset, int origin){ register long pos,realpos,count; register unsigned int f;
- register int rv; f=(fp->_flag&=~_IOEOF); count=fp->_cnt; if ((f & _IOWRT) || (count==0) || (origin==SEEK_END))
- { rv=fflush(fp); return(((rv==EOF) || (Fseek(offset,fp->_file,origin)<0)) ? -1 : 0); } if ((realpos=Fseek(0l,fp->_file,SEEK_CUR))<0) return(-1); pos=offset + count - realpos; if ((!(f & _IORW)) && (pos<=count) && (pos>=(fp->_base-fp->_ptr)))
- {
- fp->_ptr+=pos; fp->_cnt-=pos; return(0); }
- else
- {
- fp->_ptr=fp->_base; fp->_cnt=0; if (f & _IORW)
- fp->_flag=(f & (~_IOREAD));
- return((Fseek(offset,fp->_file,origin)<0) ? -1 : 0);
- }}
- int sseek(FILE *file,long pos,int end)
- {
- if (pos>=arclen || LSeek(file,pos,SEEK_SET))
- {
- if (end)
- LSeek(file,0l,SEEK_END);
- return(-1);
- }
- else
- return(0);
- }
-
- void message(uchar *p,uchar *q)
- {
- if (!flg_q)
- printf("%s: %s\n",p,q);
- }
-
- #if __030
- void get_cpu(long cookie, long *value)
- {
- register long old_stack,*cookiejar;
-
- cpu=0;
-
- old_stack = Super (NULL);
- cookiejar = *((long **) 0x5a0l);
- Super ((void *) old_stack);
-
- if (cookiejar)
- {
- while (*cookiejar)
- {
- if (*cookiejar==(long) '_CPU')
- {
- cpu=(int) (*++cookiejar);
- return;
- }
- cookiejar += 2;
- }
- }
- }
- #endif
-
- int slash(uchar *path,int set)
- {
- register int len;
-
- if (path)
- if ((len=(int) strlen(path) - 1)>=0)
- {
- path+=len;
-
- if (*path++!='\\')
- {
- if (set>0)
- {
- *path++='\\';
- *path++='\0';
- }
- else
- return(SUCCS);
- }
- else if (!set)
- *--path='\0';
- }
-
- return(FAULT);
- }
-
- uchar _proc_str[]="\r : %3d%% (%Nld/%Nld)";
- uchar _frozen_str[]="\r : %Nld -> %Nld (%3d%%) \n";
-
- void proc_ind(void)
- {
- if ((act_len+=blocksize)>=file_len || blkcnt<=0)
- printf(_proc_str,100,file_len,file_len);
- else
- printf(_proc_str,(int) ((act_len*100l)/file_len),act_len,file_len);
- fflush(stdout);
- }
-
- int arc_ext(uchar *x)
- {
- x++;
-
- if (stricmp("LZH",x) && stricmp("LHA",x) && stricmp("LZS",x))
- return(FAULT);
- else
- return(SUCCS);
- }
-
- int path_conf(uchar *path,int mode)
- {
- if (get_fname(path)>path)
- return((int) pathconf(path,mode));
- else
- return((int) pathconf(act_dir,mode));
- }
-
- int case_sensitive(uchar *path)
- {
- if (path_conf(path,-1)<_PC_CASE)
- return(_PC_CASECONV);
- else
- return((int) path_conf(path,_PC_CASE));
- }
-
- #ifndef __SHELL__
- void wait_for_key(int newline)
- {
- if (flg_h && i_handle<0 && o_handle<0)
- {
- printf(M_PRESSKEY);
- fflush(stdout);
-
- fflush(stdin);
- getch();
-
- if (newline)
- new_line();
- }
- }
- #endif
-
- void lha_exit(void)
- {
- #ifndef __SHELL__
- if (i_handle>=0)
- {
- if (i_dev>=0)
- Fforce(0,(int) i_dev);
- Fclose((int) i_handle);
- }
-
- if (o_handle>=0)
- {
- if (o_dev>=0)
- Fforce(1,(int) o_dev);
- Fclose((int) o_handle);
- }
-
- if (flg_K)
- {
- register long time=clock()+(CLK_TCK>>2);
-
- for (flg_K<<=2;flg_K>0;flg_K--)
- {
- while (clock()<time);
- time+=(CLK_TCK>>2);
- Cconout(7);
- }
- }
-
- wait_for_key(1);
- exit(errorlevel);
- #endif
- }
-
- #define close(f) if (f!=NULL) fclose(f);f=NULL
-
- void error(int errcode,uchar *p,int err)
- {
- if (err && old_afx)
- afxonoff(old_afx);
-
- if (copying)
- {
- if (!flg_q)
- printf("\n%s\n",M_COPYERR);
- close(file1);
- unlink(arcname);
- }
-
- if (!flg_q)
- {
- new_line();
-
- if (p)
- printf("%s: %s\n",errmes[errcode],p);
- else
- puts(errmes[errcode]);
- }
-
- if (file3 && (err || cmd!='P'))
- {
- close(file3);
- if (!cmdupdate && cmd!='C')
- unlink(pathname);
- }
-
- if (err)
- {
- if (file1)
- {
- close(file1);
- if (!Device && back_1)
- rename(backup1,arcname);
- }
-
- if (file2)
- {
- close(file2);
- if (!Device && (back_2 || cmd=='C'))
- {
- if (copying)
- {
- register uchar path[MAXPATH];
- strcpy(backpath(strcpy(path,backup2)),get_fname(arcname));
- rename(backup2,path);
- }
- else
- unlink(backup2);
- }
- }
- }
-
- switch (errcode)
- {
- case MEMOVRERR:
- errorlevel|=512;
- break;
- case RDERR:
- case RDONLY:
- errorlevel|=8;
- break;
- case WTERR:
- errorlevel|=4;
- break;
- case RENAMEERR:
- case MKDIRERR:
- errorlevel|=16;
- break;
- case MKFILEERR:
- case MKTMPERR:
- errorlevel|=32;
- break;
- case NOFILEERR:
- case NOFNERR:
- errorlevel|=256;
- break;
- case NOARCERR:
- case NOARCNMERR:
- errorlevel|=128;
- }
-
- if (copying)
- errorlevel|=32;
-
- if (err==SUCCS)
- lha_exit();
- }
-
- FILE *e_fopen(uchar *fname,uchar *buffer,uchar *mode,int errID,int err)
- {
- register FILE *f;
-
- if ((f=fopen(fname,mode))==NULL)
- {
- if (errno==EACCES)
- error(RDONLY,fname,err);
- else
- error(errID,fname,err);
- }
- else if (buffer)
- setvbuf(f,buffer,_IOFBF,bsize);
-
- return(f);
- }
-
- uint get_key(uchar *keys)
- {
- register uchar key;
-
- fflush(stdout);
- do
- {
- key=(uchar) toupper((int) getch());
- #if GERMAN
- if (key=='J')
- key='Y';
- #endif
- } while (!strchr(keys,key));
-
- printf("%c\n",key);
- return(key);
- }
-
- void ShipOut(void)
- {
- ship++;
- shipout();
- ship=0;
- }
-
- void tstpat(void)
- {
- register int i;
-
- if (!Nfile)
- {
- if (!flg_q)
- puts(M_NOFILEERR);
- errorlevel|=256;
- return;
- }
-
- for (i=patno;--i>=0;)
- if (!patcnt[i] && !travel_wild[i])
- {
- if (!flg_q)
- printf("%s: %s\n",M_NOMATCHERR,&fileregbuf[travel_file[i]]);
- errorlevel|=256;
- }
- }
-
- void sethdr(uchar *fn,uint attr,_DOSTIME *time,LzHead *h,int name)
- {
- register uchar *id;
- register uint l;
-
- memset(h,0,sizeof(LzHead));
-
- l=(uint) strlen(fn);
- if (name==SUCCS && flg_x==2 && *fn!='\\')
- {
- h->Fname[1]='\\';
- memcpy(h->Fname+2,fn,l++);
- }
- else
- memcpy(h->Fname+1,fn,l);
-
- if ((attr & FA_DIR) && (h->Fname[l]!='\\'))
- h->Fname[++l]='\\';
-
- h->Fname[0]=l;
- h->Attr=attr;
- h->Level=flg_k>0 ? flg_k : 0;
- h->Ftime=*time;
- h->HeadSiz=l+22+ex_len;
-
- if (file3)
- {
- h->OrgSiz=textsize=Fseek(0l,file3->_file,SEEK_END);
- Fseek(0l,file3->_file,SEEK_SET);
- }
- else
- h->OrgSiz=textsize=0;
-
- method=FlgMethod;
- codesize=compsize=0;
-
- if (attr & FA_DIR)
- id="-lhd-";
- else if (cmd=='C')
- id="-afx-";
- else if (!method)
- id="-lz5-";
- else if (method==1)
- id="-lh1-";
- else if (method==5)
- id="-lh5-";
- else
- id="-lz4-";
-
- memcpy(h->HeadID,id,5);
- }
-
- uint make_ext(uchar *dest,uchar *source,uchar id,int last)
- {
- register uint len;
-
- if ((len=(uint) strlen(source))>0)
- {
- if (last)
- len+=4;
- else
- len+=3;
-
- *dest++=(uchar) (len&0xff);
- *dest++=(uchar) (len>>8);
- *dest++=id;
-
- while (*source!='\0')
- *dest++=*source++;
-
- if (last)
- *dest++=0xff;
- }
-
- return(len);
- }
-
- int wthdr(LzHead *h,FILE *file,int rec,int make,int Case,int unpck)
- {
- register uchar *ptr,*s,*n=h->Fname+1;
- register uint len=0;
- register ulong size;
- register _DOSTIME time;
-
- if (make)
- {
- if (flg_k>0 && (UnixArc || flg_s))
- {
- register uchar c,s='\\';
-
- ptr=n;
- while ((c=*ptr++)!='\0')
- if (c==s)
- ptr[-1]='/';
-
- if (Case==_PC_CASECONV)
- strlwr(n);
- }
- else
- {
- yen2slash(n);
-
- if (Case==_PC_CASECONV)
- strupr(n);
- }
-
- if (flg_k>0)
- {
- if (flg_k<2)
- s=n+h->Fname[0]+ex_len;
- else
- {
- uchar path[MAXPATH];
-
- strncpy(path,n,h->Fname[0]);
- path[h->Fname[0]]='\0';
-
- len+=make_ext(&h->Fname[3],get_fname(path),0x01,FAULT);
-
- if ((s=backpath(path))>path)
- {
- s[-1]='\0';
- len+=make_ext(&h->Fname[len+3],path,0x02,SUCCS);
- }
-
- s=&h->Fname[len+3];
- }
-
- if (h->Attr!=0x20 || flg_k==2)
- {
- len+=5;
- *s++=5;
- *s++='\0';
- *s++=0x40;
- *s++=h->Attr;
- *s++='\0';
- }
-
- if (*comment)
- {
- register uint off=make_ext(s,comment,0x3f,SUCCS);
- len+=off;
- s+=off;
- }
-
- *s++='\0';
- *s++='\0';
- }
-
- hdr_len=len;
- }
- else
- len=hdr_len;
-
- if (flg_k==1)
- h->PacSiz+=len;
-
- if (flg_k<2)
- ptr=n+h->Fname[0];
- else
- ptr=h->Fname;
-
- *ptr++=(uchar) (crc&0xff);
- *ptr++=(uchar) (crc>>8);
-
- if (flg_k>0)
- {
- if (UnixArc || flg_s)
- *ptr=(uchar) 'U';
- else
- *ptr=(uchar) 'a';
- h->Attr=0x20;
- }
-
- if (flg_k<2)
- {
- len+=h->HeadSiz;
- h->HeadChk=mksum(h,h->HeadSiz+1);
- len+=2;
- }
- else
- {
- len+=26;
- h->HeadSiz=(uchar) (len&0xff);
- h->HeadChk=(uchar) (len>>8);
- }
-
- size=header_len=len;
-
- if (unpck)
- size+=h->OrgSiz;
- else
- size+=(h->OrgSiz>>1);
-
- ilong(&h->PacSiz);
- ilong(&h->OrgSiz);
-
- time=h->Ftime;
- if (flg_k<2)
- ITIME(h->Ftime);
- else
- dos_2_unixtime((void *) &h->Ftime);
-
- if (make && unpck && size>=bsize)
- {
- if (buffered)
- {
- ShipOut();
- LSeek(file,arcpos0,SEEK_SET);
- }
-
- rec=PAC_NO_BUF;
- }
-
- if (rec==PAC_NO_BUF)
- {
- buffered=0;
- if (fwrite((uchar *) h,7,1,file)!=1 || fwrite((uchar *) h+8,(ulong) len-7,1,file)!=1)
- error(WTERR,arcname,SUCCS);
- }
- else
- {
- if (rec==PAC_INIT_BUF)
- {
- if (file->_cnt && fflush(file))
- error(WTERR,arcname,SUCCS);
-
- if (!buffered)
- OpenOut();
- else if (outrec.cnt<=(len+32) || (bsize>size && outrec.cnt<=size))
- ShipOut();
-
- buffer_start=outrec.ptr;
- buffer_last=(outrec.ptr+=len);
- outrec.cnt-=len;
- buffered=1;
- }
-
- memcpy(buffer_start,h,7);
- memcpy(buffer_start+7,((uchar *) h)+8,len-7);
- }
-
- arcpos1=arcpos0 + len;
-
- ilong(&h->PacSiz);
- ilong(&h->OrgSiz);
- h->Ftime=time;
-
- return(rec);
- }
-
- void unix2dos(uchar *unx,int rel){ register uchar *u=unx,*d=u,c;
- yen2slash(unx); if (!strncmp(u,"\\dev\\",5))
- { u+=5; if (u[0]!='\0' && (u[1]=='\0' || u[1]=='\\'))
- {
- if (rel)
- u++;
- else
- {
- *d++=*u++;
- *d++=':';
- }
- }
- }
- else if (!strncmp(u,"\\pipe\\",6))
- u+=6; while ((c=*u++)!='\0')
- {
- if (rel && c==':' && ((d>&unx[1] && d[-2]=='\\') || (d==&unx[1])))
- d=unx; else *d++=c; }
- *d++='\0';
- }
-
- void yen2slash(uchar *p)
- {
- register uchar c,s='/';
-
- while ((c=*p++)!='\0')
- if (c==s)
- p[-1]='\\';
- }
-
- void Convert_Filename(uchar *dest,uchar *source,int conv)
- {
- if (source)
- strcpy(dest,source);
- yen2slash(dest);
-
- if (conv<0)
- Case=case_sensitive(dest);
- else if (conv>0)
- TruncFile(dest);
-
- if (!flg_S && Case==_PC_CASECONV)
- strupr(dest);
- else if (conv<0 && (flg_S==2 || (!flg_S && Case==_PC_CASEINSENS)))
- strupr(dest);
- }
-
- void unix_2_dostime(void *time)
- {
- register _DOSTIME *dostime=(_DOSTIME *) time;
- register struct tm *tm;
-
- ilong((ulong *) time);
- if ((tm=localtime((time_t *) time))!=NULL)
- {
- dostime->time=(uint) ((tm->tm_hour<<11)|(tm->tm_min<<5)|(tm->tm_sec>>1));
- dostime->date=(uint) (((tm->tm_year-80)<<9)|(tm->tm_mon<<5)|tm->tm_mday);
- }
- else
- dostime->time=dostime->date=0;
- }
-
- void dos_2_unixtime(void *time)
- {
- register _DOSTIME *dostime=(_DOSTIME *) time;
- register uint val;
- register struct tm tm,*t=&tm;
-
- val=dostime->time;
- t->tm_sec=(val&31)<<1;
- t->tm_min=(val>>=5)&63;
- t->tm_hour=val>>=6;
-
- val=dostime->date;
- t->tm_mday=val&31;
- t->tm_mon=(val>>=5)&15;
- t->tm_year=(val>>=4)+80;
-
- *((time_t *) time)=mktime(t);
- ilong((ulong *) time);
- }
-
- uchar *gethdr(FILE *arc,LzHead *h)
- {
- uchar exthdr[MAXEXT],fname[MAXPATH];
- register uchar *p;
- register uint extsize;
- register int length;
- register long pos,read;
-
- _gethdr:
- *comment='\0';
- lastarcpos=nextarcpos;
- memset(h,0,sizeof(LzHead));
-
- if (fread((uchar *) h,7,1,arc)!=1 || fread((uchar *) h+8,14,1,arc)!=1)
- goto no_hdr;
-
- if (h->Level>2)
- goto no_hdr;
- else if (h->Level==2)
- h->HeadSiz=HDRSIZ2;
- else if (h->HeadSiz<MINHDR || h->HeadSiz>sizeof(LzHead))
- goto no_hdr;
-
- read=h->HeadSiz+2;
-
- if (fread((uchar *) h+22,read-21,1,arc)!=1)
- goto no_hdr;
-
- ilong(&h->PacSiz);
- ilong(&h->OrgSiz);
-
- if (h->Level<2)
- ITIME(h->Ftime);
- else
- unix_2_dostime((void *) &h->Ftime);
- #if 0
- h->Mtime=h->Ftime;
- #endif
-
- lastarclen=read+h->PacSiz;
- pos=lastarcpos+read;
-
- if (h->Level<2)
- {
- if (mksum(h,h->HeadSiz)!=h->HeadChk && mksum(h,h->HeadSiz+1)!=h->HeadChk)
- goto no_hdr;
-
- length=h->Fname[0];
- strncpy(filename,&h->Fname[1],length);
- filename[length++]='\0';
- }
- else
- {
- length=0;
- filename[0]='\0';
- }
-
- p=h->Fname+length;
- h->crc=(uint) *p++;
- h->crc|=(uint) (*p++<<8);
- SystemId=*p++;
-
- has_crc=(flg_chk) ? FAULT : SUCCS;
-
- switch(h->HeadSiz-length)
- {
- case (HDRSIZ0-1-2):
- has_crc=FAULT;
- case (HDRSIZ0-1):
- h->Level=0;
- break;
- default:
- if (h->Level==0)
- {
- if (SystemId==EXTEND_UNIX || SystemId==EXTEND_OS68K)
- {
- #if 0
- memcpy(&h->Mtime,p+1,4);
- unix_2_dostime((void *) &h->Mtime);
- #endif
- }
- else
- h->Level++;
- }
- break;
- }
-
- if (h->Level>0)
- {
- extsize=(uint) *p++;
- extsize|=(uint) (*p++<<8);
-
- while (extsize>0)
- {
- pos+=extsize;
- if (extsize>MAXEXT)
- {
- if (sseek(arc,pos-2,0))
- goto no_hdr;
- }
- else if (fread(exthdr,(length=extsize-2),1,arc)!=1)
- goto no_hdr;
- else
- {
- length--;
- switch (exthdr[0])
- {
- #if 0
- case 0: /* common header */
- break;
- #endif
- case 1: /* filename header */
- case 2: /* pathname header */
- if (length>=pt_name)
- break;
-
- strncpy(fname,&exthdr[1],length);
-
- if ((p=strchr(fname,'\xff'))!=NULL)
- length=(int) (p-fname);
-
- fname[length]='\0';
-
- if (exthdr[0]==1)
- {
- p=get_fname(filename);
- if (((int) (p - filename) + length)<pt_name)
- strcpy(p,fname);
- }
- else if (((int) strlen(filename) + length + 1)<pt_name)
- {
- if (fname[length-1]!='\\' && fname[length-1]!='/')
- strcat(fname,"\\");
-
- strcat(fname,filename);
- strcpy(filename,fname);
- }
- break;
- case 0x3f: /* comment */
- strncpy(comment,&exthdr[1],length);
- comment[length]='\0';
-
- if ((p=strchr(comment,'\xff'))!=NULL)
- *p='\0';
- break;
- case 0x40: /* file attribute */
- h->Attr=exthdr[1];
- break;
- #if 0
- case 0x50: /* permission */
- case 0x51: /* gid and uid */
- case 0x52: /* group name */
- case 0x53: /* user name */
- break;
- case 0x54: /* last modified time */
- memcpy(&h->Mtime,&exthdr[1],4);
- unix_2_dostime((void *) &h->Mtime);
- break;
- #endif
- }
- }
-
- read+=extsize;
- if (h->Level<2)
- h->PacSiz-=extsize;
- else
- lastarclen+=extsize;
-
- extsize=Bgetc(arc);
- extsize+=(uint) Bgetc(arc)<<8;
- }
- }
-
- if (strchr("UXHK",SystemId) || strchr(filename,'/'))
- {
- UnixArc=SUCCS;
-
- if (flg_k<=0)
- {
- flg_k=1;
- ex_len=3;
- }
- }
-
- yen2slash(filename);
- unix2dos(strcpy(matchfilename,filename),1);
-
- if (matchfilename[0] && matchfilename[1]==':')
- strcpy(matchfilename,matchfilename+2);
-
- if (matchfilename[0]=='\\' && flg_x==3)
- strcpy(matchfilename,matchfilename+1);
-
- if (!strncmp(matchfilename,".\\",2))
- strcpy(matchfilename,matchfilename+2);
- else if (!strncmp(matchfilename,"..\\",3))
- strcpy(matchfilename,matchfilename+3);
-
- h->Attr&=0x3f;
-
- {
- register int len=(int) strlen(matchfilename) - 1;
-
- if (matchfilename[len]=='\\')
- {
- matchfilename[len]='\0';
- h->Attr=FA_DIR;
- }
- else if (tstID(h->HeadID)==6)
- h->Attr=FA_DIR;
-
- if (h->Attr & FA_DIR)
- {
- if (h->Attr!=FA_DIR)
- h->Attr&=~FA_DIR;
- else
- {
- h->OrgSiz=h->PacSiz=0;
- lastarclen=read;
- }
- }
- }
-
- nextarcpos=lastarcpos+lastarclen;
-
- if (!flg_S && Case==_PC_CASECONV)
- strupr(matchfilename);
-
- Convert_Filename(dosfilename,matchfilename,1);
- UnixFile=strcmp(dosfilename,matchfilename);
-
- if (flg_S==2 && (!flg_S && Case==_PC_CASEINSENS))
- strupr(matchfilename);
-
- if (!ferror(arc))
- return(get_fname(matchfilename));
-
- no_hdr:
- if (lastarcpos<(arclen-20))
- {
- LSeek(arc,lastarcpos,SEEK_SET);
-
- if (search_lzh(arc,NULL,-1))
- {
- if (!flg_q)
- #if GERMAN
- puts(" Zerstörte Daten gefunden und überlesen");
- #else
- puts(" Garbage found and skipped");
- #endif
- garbage++;
- goto _gethdr;
- }
- else if (!flg_q)
- #if GERMAN
- printf("Überflüssige Daten am Archivende (%ld Bytes)\n",arclen-lastarcpos);
- #else
- printf("Garbage found at the archive-end (%ld bytes)\n",arclen-lastarcpos);
- #endif
- }
-
- return(NULL);
- }
-
- int matchpat(uchar *p,register int pat,register int attr)
- {
- register uchar *name,path[MAXPATH];
- register int i,k,retcode=FAULT,wild;
-
- if (*p=='\0')
- return(FAULT);
-
- if (attr & FA_DIR)
- {
- if (flg_f<2)
- {
- Nfile++;
- return(SUCCS);
- }
- }
- else if (flg_f==3)
- return(FAULT);
-
- name=get_fname(p);
- backpath(strcpy(path,p));
-
- for (i=(pat>=0) ? (pat+1) : patno;--i>=0;)
- {
- wild=(flg_W) ? flg_W : !travel_file[i];
-
- if (flg_p)
- {
- if (strcmp(path,travel_path[i]))
- continue;
- }
- else if (travel_len[i] && strncmp(path,travel_path[i],travel_len[i]))
- continue;
-
- if (all || chk_wild(name,&fileregbuf[travel_file[i]],wild))
- {
- for (k=exno;--k>=0;)
- if (chk_wild(name,exclude_file[k],0))
- break;
-
- if (k<0)
- {
- if (pat<0)
- {
- patcnt[i]++;
- retcode=SUCCS;
- }
- else
- return(SUCCS);
- }
- }
-
- if (pat>=0)
- break;
- }
-
- if (retcode)
- Nfile++;
-
- return(retcode);
- }
-
- uint ratio(ulong a,ulong b)
- {
- register int i;
-
- if (!b)
- return(1000);
-
- for (i=0; i<3 && a<0x19999999L; i++)
- a*=10;
- for (;i<3;i++)
- b/=10;
- a+=b/2;
- return((uint) (a/b));
- }
-
- void regfile(uchar *p,uchar *q,_DTA *file,uchar *f)
- {
- register uchar *s;
- register filebuf *f0,*f1;
- register int attr,len;
- register uchar ch;
-
- attr=(oldtos) ? file->dta_attribute^FA_CHANGED : file->dta_attribute;
- if (attr & FA_DIR)
- attr=FA_DIR;
- else
- {
- if (flg_arc && !(attr & FA_CHANGED))
- return;
- else if (flg_j && file->dta_size==0)
- return;
- else if (maxlen>0 && file->dta_size>maxlen)
- return;
- else if (cmd=='C' && file->dta_size<=min_len)
- return;
- }
-
- if (strstr(f,TEMPFILE))
- return;
-
- if (!fbuf)
- {
- if ((fbmax=((long) Malloc(-1)))>65536L && flg_L<2)
- fbmax>>=1;
- fbuf=fbnxt=Malloc(fbmax);
-
- if (!fbuf)
- error(MEMOVRERR,NULL,SUCCS);
-
- *((long *) fbuf)=0l;
- fblft=(fbmax-=256)-4;
- fbnxt+=4;
- fblast=(filebuf *) fbuf;
- }
-
- f1=(filebuf *) fbnxt;
- strcpy(s=stpcpy(f1->dir,p),f);
-
- if (o_dir!=NULL && !strcmp(f1->dir,o_dir))
- return;
-
- f1->time.time=file->dta_time;
- f1->time.date=file->dta_date;
-
- if (flg_N && FTimeToULong(&newer)>FTimeToULong(&f1->time))
- return;
-
- f1->next=NULL;
- f1->fpos=s-(uchar *) f1;
- f1->cpos=flg_x ? ((q-p)+f1->dir-(uchar *) f1) : f1->fpos;
- f1->attr=attr;
- f1->flag=0;
- f1->Case=Case;
- f1->cluster=min_len;
-
- if (patno>1 || !flg_x)
- {
- f=(uchar *) f1+f1->fpos;
- f0=(filebuf *) fbuf;
- ch=*f;
-
- while ((f0=f0->next)!=NULL)
- {
- p=(uchar *) f0+f0->fpos;
- if (ch==*p && !strcmp(f,p) && !strcmp((uchar *) f0+f0->cpos,(uchar *) f1+f1->cpos))
- return;
- }
- }
-
- if (fblft<sizeof(filebuf))
- {
- ignfile++;
- return;
- }
-
- len=(int) (strlen(f1->dir)+(f1->dir-(uchar *) f1))+1;
- len+=len & 1;
-
- fblast->next=fbnxt;
-
- fblast=(filebuf *) fbnxt;
- fblft-=len;
- fbnxt+=len;
- fbfiles++;
-
- if (flg_n!=1 && !(fbfiles & 15))
- {
- printf("\b\b\b\b%4d",fbfiles);
- fflush(stdout);
- }
- }
-
- void travel(uchar *p,uchar *q,int pat)
- {
- _DTA dta,*old;
- register _DTA *d=&dta;
- register int done,compare,filesys;
- register long handle,buf[MAXPATH>>2];
- register uchar *s,*name;
- register XATTR *x=&xattr;
-
- filesys=(!flg_S && Case==_PC_CASECONV);
- compare=(flg_S==2 || (!flg_S && Case==_PC_CASEINSENS));
-
- if (!_gemdos)
- {
- name=(uchar *) &buf[1];
-
- if (*p!='\0')
- handle=Dopendir(p,0);
- else
- handle=Dopendir(act_dir,0);
-
- if ((handle&0xff000000l)==0xff000000l)
- return;
- else
- done=(int) Dreaddir(MAXPATH,handle,(uchar *) buf);
- }
- else
- {
- name=d->dta_name;
-
- strcat(p,"*.*");
-
- old=Fgetdta();
- Fsetdta(d);
- done=Fsfirst(p,-1);
- }
-
- s=backpath(p);
-
- while (!done)
- {
- if (name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0')))
- goto travel_next;
- else
- {
- if (!_gemdos)
- {
- strcpy(s,name);
- Fxattr(0,p,(uchar *) x);
-
- d->dta_time=x->ctime;
- d->dta_date=x->cdate;
- d->dta_size=x->size;
- d->dta_attribute=x->attr;
- }
-
- if ((!flg_a && (d->dta_attribute & (FA_HIDDEN|FA_SYSTEM|FA_RDONLY))) || (d->dta_attribute & FA_LABEL))
- goto travel_next;
- }
-
- if (d->dta_attribute & FA_DIR)
- {
- if (filesys)
- strupr(name);
-
- if (flg_f>1)
- {
- strcpy(s,name);
- if (compare)
- strupr(s);
-
- if (matchpat(p,pat,FA_DIR))
- {
- *s='\0';
- regfile(p,q,d,name);
- }
- }
- else if (flg_f && travel_wild[pat])
- {
- *s='\0';
- regfile(p,q,d,name);
- }
-
- if (flg_r && travel_wild[pat])
- {
- strcpy(stpcpy(s,name),"\\");
-
- if (strlen(p)>=pt_name)
- message(M_TOOLONGERR,p);
- else
- travel(p,q,pat);
- }
- }
- else if (flg_f<3)
- {
- if (filesys)
- strupr(name);
- strcpy(s,name);
- if (compare)
- strupr(s);
-
- if (matchpat(p,pat,0))
- {
- *s='\0';
- regfile(p,q,d,name);
- }
- }
-
- travel_next:
-
- if (_gemdos)
- done=Fsnext();
- else
- done=(int) Dreaddir(MAXPATH,handle,(uchar *) buf);
- *s='\0';
-
- }
-
- if (_gemdos)
- Fsetdta(old);
- else
- Dclosedir(handle);
- }
-
- int drive(uchar *path)
- {
- if (path[0] && path[1]==':')
- {
- register int drv=toupper(path[0]);
-
- if (__mint && drv=='U')
- {
- if (path[2]=='\\' && path[3] && path[4]=='\\')
- drv=toupper(path[3]);
- }
-
- if (drv>='A' && drv<='Z')
- return(drv-'A');
- }
-
- return(Dgetdrv());
- }
-
- void mklist(void)
- {
- register _BPB *bpb;
- register uchar path[MAXPATH],*rel;
- register int i,new;
-
- fbfiles=Nfile=0;
-
- for (i=0;i<patno;i++)
- {
- if (!flg_q)
- {
- #if GERMAN
- printf("\r Suchmuster : (%d/%d), Dateien gefunden : %4d",i+1,patno,Nfile);
- #else
- printf("\r Pattern : (%d/%d), Files matched : %4d",i+1,patno,Nfile);
- #endif
- fflush(stdout);
- }
-
- if (!i)
- new=SUCCS;
- else
- new=strcmp(path,travel_path[i]);
-
- strcpy(rel=path,travel_path[i]);
-
- if (cmd=='C' && new && (bpb=Getbpb(drive(path)))!=NULL)
- min_len=bpb->clsizb;
- else
- min_len=1024;
-
- if (pack || flg_X)
- rel+=strlen(path);
- else if (travel_rel[i])
- rel+=travel_rel[i];
- else
- {
- if (*rel && rel[1]==':')
- rel+=2;
-
- if (*rel=='\\' && flg_x==3)
- rel++;
-
- if (!strncmp(rel,".\\",2))
- rel+=2;
- else if (!strncmp(rel,"..\\",3))
- rel+=3;
- }
-
- if (new)
- {
- pt_name=path_conf(path,_PC_PATH_MAX);
- if (pt_name>MAXPATH)
- pt_name=MAXPATH;
- Case=case_sensitive(path);
-
- if (__mint)
- _gemdos=(path_conf(path,_PC_NAME_MAX)<=12 && Case==_PC_CASECONV) ? 1 : 0;
- else
- _gemdos=1;
- }
-
- travel(path,rel,i);
- patcnt[i]=fbfiles-Nfile;
- Nfile=fbfiles;
- }
-
- Mshrink(fbuf,fbmax-fblft+32);
-
- if (!flg_q)
- {
- printf("\b\b\b\b%4d\n\n",Nfile);
- if (ignfile)
- #if GERMAN
- printf("Datei-Tabelle voll, %d Datei(en) ignoriert\n",ignfile);
- #else
- printf("File table overflow, %d file(s) ignored\n",ignfile);
- #endif
- }
- }
-
- long _proc_size[]={1000000L,
- 10000000L,
- 100000000L,
- 1000000000L};
-
- void blkdisp(long len,uchar *s,long size)
- {
- register int i;
-
- blocksize=size;
- blkcnt=(int) ((len+size-1)/size);
-
- for (i=0;i<(sizeof(_proc_size)>>2);i++)
- if (len<_proc_size[i])
- break;
-
- _frozen_str[13]=_frozen_str[21]=(i+'6');
-
- if (!flg_n)
- {
- register uchar pind[MAXPATH],*p=pind,c=pnt;
-
- if (blkcnt>maxblk)
- {
- blkcnt=maxblk;
- blocksize=len/maxblk;
- }
- else if (!blkcnt)
- blkcnt++;
-
- for (i=blkcnt;--i>=0;)
- *p++=c;
- *p='\0';
-
- printf(" %s : %s\r %s : ",s,pind,s);
- }
- else if (flg_n==2)
- {
- memcpy(_proc_str+2,s,8);
- _proc_str[20]=_proc_str[25]=_frozen_str[13];
- printf(_proc_str,0,act_len=0,file_len=len);
- }
- else if (flg_n==3)
- {
- RotInd=0;
- printf(" %s: -\b",s);
- }
-
- fflush(stdout);
- }
-
- void MakeBuffers(void)
- {
- register long buf,len=0;
-
- if (cmdupdate && !flg_u && FlgMethod==5)
- {
- if ((buf=(long) malloc(ENCODE5))!=0)
- {
- buf=(buf+=63) & (~15L);
-
- text=(uchar *) buf;
- level=(uchar *) (buf+=TEXT);
- childcount=(uchar *) (buf+=LEVEL);
- position=(short *) (buf+=CHILDCOUNT);
- parent=(short *) (buf+=POSITION);
- prev=(short *) (buf+=PARENT);
- next=(short *) (buf+=PREV);
- }
- else
- error(MEMOVRERR,NULL,SUCCS);
- }
- else if (FlgMethod!=5 || !cmdupdate)
- {
- if ((buf=(long) malloc(ENCODE))!=0)
- {
- buf=(buf+=63) & (~15L);
-
- lson=(int *) buf;
- rson=(int *) (buf+=LSON);
- dad=(int *) (buf+=RSON);
- }
- else
- error(MEMOVRERR,NULL,SUCCS);
- }
-
- if (!flg_L)
- {
- len=((long) Malloc(-1)>>3) & (~1023l);
- if (len>MAXBUFFER)
- len=MAXBUFFER;
- }
- else if (flg_L==2)
- {
- len=(((long) Malloc(-1)-65536L)>>2) & (~1023l);
- if (len>MAXBUFFERL2)
- len=MAXBUFFERL2;
- }
-
- if (len<=BUFFERSIZ || (buf=(long) malloc(((len+=128)<<2)+128))==0)
- {
- buf=(long) buffer;
- len=BUFFERSIZ+128;
- }
-
- buffer_1=(uchar *) ((buf+63) & (~15l));
- buffer_2=buffer_1+len;
- buffer_3=buffer_2+len;
- buffer_gen=buffer_3+len;
- bsize=len-128;
- }
-
- void freeze(uchar *p,int attr,int file,int Case)
- {
- register int rec=PAC_INIT_BUF,unpck=(flg_u || (Hdr2.OrgSiz<=40 && !flg_5));
- register long arcpos;
-
- crc=ship=0;
-
- if (!unpck && flg_U && !flg_5 && chk_wild(get_fname(p),unpack,0))
- unpck=SUCCS;
-
- if (FTimeToULong(&arcstamp)<FTimeToULong(&Hdr2.Ftime))
- arcstamp=Hdr2.Ftime;
-
- if (!flg_q)
- {
- if (file>0)
- printf("(%d/%d): ",file,Nfile);
-
- if (attr & FA_DIR)
- printf("%s: Directory\n",p);
- else
- puts(p);
-
- if (flg_e)
- get_comment(stdin);
- }
-
- blkdisp(Hdr2.OrgSiz,(unpck || (attr & FA_DIR)) ? "Storing " : "Freezing",(method==5 && !unpck) ? (N*2) : N);
-
- if (attr & FA_DIR)
- {
- Hdr2.PacSiz=Hdr2.OrgSiz=0;
- wthdr(&Hdr2,file2,rec,SUCCS,Case,SUCCS);
- arcpos0=arcpos1;
- ProcInd();
- }
- else
- {
- if (!unpck || cmd!='C')
- rec=wthdr(&Hdr2,file2,rec,SUCCS,Case,unpck);
-
- origsize=textsize=Hdr2.OrgSiz;
-
- infile=file3;
- infname=p;
-
- if (unpck)
- codesize=textsize+1;
- else if (!method)
- EncodeOld();
- else if (method==1)
- Encode();
- else if (method==5)
- {
- init_encode5();
- encode5();
- codesize=compsize;
- }
-
- if (flg_backup)
- {
- register int new_attr=attr & (~FA_CHANGED);
- if (attr!=new_attr)
- Fattrib(infname,1,(oldtos) ? new_attr^FA_CHANGED : new_attr);
- }
-
- if (!buffered)
- rec=PAC_NO_BUF;
-
- if (codesize>=origsize || (cmd=='C' && ((origsize+min_len-1)/min_len)<=((codesize+header_len+min_len-1)/min_len)))
- {
- flg_unpacked=1;
-
- if (cmd!='C')
- {
- arcpos=arcpos1 + Hdr2.OrgSiz;
- Hdr2.PacSiz=Hdr2.OrgSiz;
-
- if (!flg_4)
- memcpy(Hdr2.HeadID,"-lh0-",5);
-
- if (!unpck)
- {
- if (buffered)
- {
- shipout();
- rec=PAC_NO_BUF;
- }
-
- LSeek(file2,arcpos1,SEEK_SET);
- LSeek(file3,0l,SEEK_SET);
- copyfile(file3,file2,Hdr2.OrgSiz,0,0);
- }
- else
- copyfile(file3,file2,Hdr2.OrgSiz,1,(rec==PAC_NO_BUF) ? 0 : 1);
- }
- else
- {
- while ((long) origsize>0)
- {
- ProcInd();
- origsize-=blocksize;
- }
-
- Hdr2.PacSiz=Hdr2.OrgSiz;
- goto _freeze_end;
- }
- }
- else
- {
- arcpos=arcpos1 + codesize;
- Hdr2.PacSiz=codesize;
- flg_unpacked=0;
- }
-
- if (rec==PAC_NO_BUF)
- LSeek(file2,arcpos0,SEEK_SET);
- else
- rec=PAC_EXIT_BUF;
-
- wthdr(&Hdr2,file2,rec,FAULT,Case,unpck);
- arcpos0=arcpos;
- if (cmd!='C' && (rec==PAC_NO_BUF || !buffered))
- LSeek(file2,arcpos0,SEEK_SET);
- }
-
- _freeze_end:
- if (flg_n!=1)
- {
- memcpy(_frozen_str+2,(flg_unpacked || (attr & FA_DIR)) ? "Stored: " : "Frozen: ",10);
- printf(_frozen_str,Hdr2.OrgSiz,Hdr2.PacSiz,ratio(Hdr2.PacSiz,Hdr2.OrgSiz)/10);
- }
-
- *comment='\0';
- }
-
- void get_comment(FILE *f)
- {
- register int len;
- register uchar *input=comment,*q;
-
- if (f==stdin)
- puts(M_COMMENT);
-
- while (fgets(input,255,f))
- {
- if ((q=strchr(input,'\r'))!=NULL || (q=strchr(input,'\n'))!=NULL)
- *q='\0';
-
- if ((len=(int) strlen(input))>0 || f!=stdin)
- {
- input+=len;
- *input++='\n';
- if (!flg_s)
- *input++='\r';
-
- if ((int) (input-comment)>=MAXCOMMENT)
- break;
- }
- else
- break;
- }
-
- if (input>comment)
- {
- if (flg_s)
- input[-1]='\0';
- else
- input[-2]='\0';
- }
- else
- *comment='\0';
- }
-
- void archive_comment(FILE *output)
- {
- if (com_name)
- {
- register FILE *f;
-
- if ((f=fopen(com_name,"r"))!=NULL)
- {
- get_comment(f);
- fclose(f);
- }
- }
- else
- get_comment(stdin);
-
- if (*comment)
- {
- uchar buf[MAXCOMMENT+256]=COM_ID;
- strcat(buf,comment);
-
- arcpos0=strlen(buf)+1;
- if (fwrite(buf,arcpos0,1,output)!=1)
- error(WTERR,arcname,SUCCS);
-
- *comment='\0';
- }
- }
-
- void copyold(int buf)
- {
- infname=arcname;
- if (FTimeToULong(&arcstamp)<FTimeToULong(&Hdr1.Ftime))
- arcstamp=Hdr1.Ftime;
- LSeek(file1,lastarcpos,SEEK_SET);
-
- if (buf && buffered)
- {
- if (outrec.cnt<=lastarclen)
- {
- ShipOut();
- LSeek(file2,arcpos0,SEEK_SET);
- copyfile(file1,file2,lastarclen,0,0);
- }
- else
- copyfile(file1,file2,lastarclen,0,1);
- }
- else
- copyfile(file1,file2,lastarclen,0,0);
- arcpos0+=lastarclen;
- }
-
- int execappend(void)
- {
- register filebuf *f0;
- register uchar *q;
- register int update,in_arc,cnt=0,file=1;
-
- f0=(filebuf *) fbuf;
- q=file1 ? gethdr(file1,&Hdr1) : NULL;
-
- for (;;)
- {
- update=FAULT;
-
- if (q)
- {
- in_arc=SUCCS;
- f0=(filebuf *) fbuf;
-
- while ((f0=f0->next)!=NULL)
- {
- if (!f0->flag && strcmp((uchar *) f0+f0->fpos,q)==0)
- {
- if (flg_I)
- strcpy(backpath(incldir),(uchar *) f0+f0->fpos);
-
- if (strcmp((flg_I) ? incldir : (uchar *) f0+f0->cpos,matchfilename)==0)
- {
- if (!flg_A)
- update=SUCCS;
- f0->flag=SUCCS;
- break;
- }
- }
- }
- }
- else
- {
- in_arc=FAULT;
-
- while ((f0=f0->next)!=NULL)
- {
- if (!f0->flag)
- {
- if (flg_I)
- strcpy(backpath(incldir),(uchar *) f0+f0->fpos);
- f0->flag=update=SUCCS;
- break;
- }
- }
-
- if (!f0)
- break;
- }
-
- if (!flg_q)
- {
- printf("(%d/%d):\r",file,Nfile);
- fflush(stdout);
- }
-
- if (update && (!in_arc || flg_c || (FTimeToULong(&Hdr1.Ftime)<FTimeToULong(&f0->time))))
- {
- if (f0->attr & FA_DIR)
- file3=NULL;
- else if ((file3=e_fopen(f0->dir,buffer_3,"rb",RDERR,FAULT))==NULL)
- {
- if (in_arc)
- copyold(SUCCS);
- goto _append_next;
- }
-
- sethdr((flg_I) ? incldir : (uchar *) f0+f0->cpos,f0->attr,&f0->time,&Hdr2,SUCCS);
- freeze(f0->dir,f0->attr,file,f0->Case);
- close(file3);
- cnt++;
-
- if (in_arc)
- sseek(file1,nextarcpos,1);
- }
- else
- copyold(SUCCS);
-
- _append_next:
- if (q && (q=gethdr(file1,&Hdr1))==NULL)
- f0=(filebuf *) fbuf;
-
- if (update)
- file++;
- }
-
- return(cnt);
- }
-
- void delfile(void)
- {
- filebuf *f0;
-
- f0=(filebuf *) fbuf;
- while ((f0=f0->next)!=NULL)
- {
- if (!(f0->attr & FA_DIR))
- unlink(f0->dir);
- };
- }
-
- int search_lzh(FILE *input,FILE *output,int mode)
- {
- register long len,pos;
-
- if (mode<0)
- pos=ftell(input);
- else
- pos=0;
-
- _cont_search:
- if ((len=fread(text_buf,1,4096,input))>0)
- {
- register uchar *ptr=text_buf,*last=ptr+len-5,c,s='-';
-
- if (mode>=0)
- {
- arcpos0=Fseek(0l,input->_file,SEEK_CUR);
- arclen=Fseek(0l,input->_file,SEEK_END);
- Fseek(arcpos0,input->_file,SEEK_SET);
- arcpos0=0;
- }
-
- for (;ptr<last;ptr++)
- if (*ptr==s && ptr[4]==s)
- {
- c=ptr[1];
- if (c=='l' || c=='L' || c=='a' || c=='A')
- {
- if (ptr>=(text_buf+2))
- {
- if (output && flg_z)
- archive_comment(output);
-
- LSeek(input,nextarcpos=pos+((ptr-2)-text_buf),SEEK_SET);
- INIT_TIMER;
- return(SUCCS);
- }
- }
- else if (c=='c' && ptr[2]=='o' && ptr[3]=='m')
- {
- if (output && !flg_z)
- {
- arcpos0=strlen(ptr)+1;
- if (fwrite(ptr,arcpos0,1,output)!=1)
- error(WTERR,arcname,SUCCS);
- }
-
- if (mode>0 && !flg_q && flg_x && ((cmd!='V' && cmd!='L') || flg_x<2))
- {
- puts(ptr+5);
- new_line();
- }
-
- ptr+=strlen(ptr);
- }
- }
- }
-
- if (mode>=0)
- error(NOFILEERR,arcname,(mode>0) ? 128 : SUCCS);
- else if (len>0)
- {
- pos+=len;
- goto _cont_search;
- }
-
- return(FAULT);
- }
-
- int openarc1(long size)
- {
- back_1=back_2=0;
-
- file1=e_fopen(infname=arcname,NULL,"rb",NOARCERR,FAULT);
- if (file1 && search_lzh(file1,NULL,1))
- {
- setvbuf(file1,buffer_1,_IOFBF,size);
- INIT_TIMER;
- return(SUCCS);
- }
- else
- {
- close(file1);
- return(FAULT);
- }
- }
-
- void get_tempname(uchar *path)
- {
- register uchar *ext;
-
- strcat(path,TEMPFILE);
- ext=path+strlen(path);
-
- do
- {
- sprintf(ext,"%X",temp());
- } while (!Fsfirst(path,-1));
- }
-
- void openbackup1(void)
- {
- back_1++;
-
- backpath(strcpy(backup1,arcname));
- get_tempname(backup1);
-
- if (Frename(0,arcname,infname=backup1))
- error(RENAMEERR,arcname,SUCCS);
-
- file1=e_fopen(backup1,buffer_1,"rb",WTERR,SUCCS);
- arclen=Fseek(0l,file1->_file,SEEK_END);
- Fseek(0l,file1->_file,SEEK_SET);
- }
-
- void openbackup2(void)
- {
- back_2++;
-
- if (Device)
- strcpy(backup2,arcname);
- else
- {
- if (flg_w)
- strcpy(backup2,workdir);
- else
- backpath(strcpy(backup2,arcname));
- get_tempname(backup2);
- }
-
- outfile=file2=e_fopen(outfname=backup2,buffer_2,(flg_w) ? "w+b" : "wb",MKTMPERR,SUCCS);
- INIT_TIMER;
- }
-
- void stclosearc(FILE *f)
- {
- if (fflush(f))
- error(WTERR,arcname,SUCCS);
-
- if (flg_t)
- Fdatime(&arcstamp,f->_file,1);
-
- if (fclose(f))
- error(WTERR,arcname,SUCCS);
- }
-
- void endofupdate(int cnt)
- {
- if (file1)
- {
- Fdatime(&arcstamp,file1->_file,1);
- fclose(file1);
- }
-
- tstpat();
-
- if (cnt)
- {
- if (file1)
- {
- if (flg_B)
- {
- register uchar bakname[MAXPATH],*f;
-
- strcpy(bakname,arcname);
- if ((f=strrchr(get_fname(bakname),'.'))!=NULL && (arc_ext(f) || f[1]=='\0'))
- strcpy(f,".BAK");
- else
- strcat(bakname,".BAK");
- unlink(bakname);
- rename(backup1,bakname);
- }
- else
- unlink(backup1);
- file1=NULL;
- }
-
- if (arcpos0)
- {
- if (fputc(0,file2)==EOF)
- error(WTERR,arcname,SUCCS);
-
- if (!Device)
- {
- if (flg_w && drive(arcname)!=drive(backup2))
- {
- if (!flg_q)
- #if GERMAN
- puts("Kopiere temporäres Archiv...");
- #else
- puts("Copying Temp to Archive ...");
- #endif
-
- file1=e_fopen(outfname=arcname,buffer_1,"wb",MKFILEERR,SUCCS);
- LSeek(file2,0l,SEEK_SET);
-
- copying=1;
- copyfile(file2,file1,arcpos0+1,0,0);
- stclosearc(file1);
- copying=0;
-
- close(file2);
- unlink(backup2);
- }
- else
- {
- stclosearc(file2);
- rename(backup2,arcname);
- }
- }
- }
- else
- {
- close(file2);
- if (!Device)
- unlink(backup2);
- }
- }
- else
- {
- close(file2);
- if (!Device)
- {
- unlink(backup2);
- rename(backup1,arcname);
- }
- }
- file1=file2=NULL;
- }
-
- void append(void)
- {
- register int cnt;
-
- if (Device)
- file1=NULL;
- else if ((file1=fopen(arcname,"rb"))!=NULL)
- {
- close(file1);
- openbackup1();
- }
-
- mklist();
- if (!Nfile)
- error(NOFILEERR,NULL,SUCCS);
-
- if (flg_u && !flg_n)
- flg_n++;
-
- if (file1)
- message("Updating archive",arcname);
- else if (Device)
- message("Freeze/Store to",arcname);
- else
- message("Creating archive",arcname);
-
- openbackup2();
- if (file1)
- search_lzh(file1,file2,0);
- else if (flg_z)
- archive_comment(file2);
-
- buffered=0;
- cnt=execappend();
- if (buffered)
- {
- ShipOut();
- LSeek(file2,arcpos0,SEEK_SET);
- }
-
- endofupdate(cnt);
-
- if (flg_d)
- delfile();
- }
-
- void pack_afx(void)
- {
- register filebuf *f0;
- register int file=1,tst;
- register uchar *x;
-
- flg_x=3;
- old_afx=afxonoff(0L);
-
- mklist();
- if (!Nfile)
- error(NOFILEERR,NULL,SUCCS);
-
- FlgMethod=flg_x=0;
- f0=(filebuf *) fbuf;
-
- while ((f0=f0->next)!=NULL)
- {
- if ((x=strrchr(get_fname(f0->dir),'.'))!=NULL && (!strcmp(x,".O") || !strcmp(x,".LIB")))
- obj=SUCCS;
- else
- obj=FAULT;
-
- if ((tst=test_afx(f0->dir))==0)
- {
- if ((file3=e_fopen(f0->dir,buffer_3,"rb",RDERR,FAULT))!=NULL)
- {
- strcpy(backpath(strcpy(backup2,f0->dir)),"__temp__.lzs");
- if ((outfile=file2=e_fopen(outfname=backup2,buffer_2,"wb",MKTMPERR,FAULT))!=NULL)
- {
- arcpos0=buffered=0;
- min_len=f0->cluster;
- sethdr((uchar *) f0+f0->fpos,f0->attr,&f0->time,&Hdr2,SUCCS);
- freeze(f0->dir,f0->attr,file,f0->Case);
-
- if (buffered && !flg_unpacked)
- ShipOut();
-
- close(file2);
- }
-
- close(file3);
-
- if (!flg_unpacked)
- {
- unlink(f0->dir);
- if (rename(backup2,f0->dir))
- error(RENAMEERR,f0->dir,FAULT);
- }
- else
- unlink(backup2);
- }
- }
- else
- {
- if (!flg_q)
- {
- printf("(%d/%d): %s\n ",file,Nfile,f0->dir);
-
- switch (tst)
- {
- case 1:
- #if GERMAN
- puts("Datei bereits in AFX-Format");
- #else
- puts("Already in AFX-format");
- #endif
- break;
- case 2:
- #if GERMAN
- puts("Datei bereits in LHarc-Format");
- #else
- puts("Already in LHarc-format");
- #endif
- break;
- case 3:
- #if GERMAN
- puts("Programm-Datei");
- #else
- puts("Program-file");
- #endif
- break;
- default:
- #if GERMAN
- puts("Kann Datei nicht lesen");
- #else
- puts("Read-Error");
- #endif
- }
- }
- }
- file++;
- }
-
- if (old_afx)
- {
- afxonoff(old_afx);
- old_afx=0;
- }
- }
-
- void make_fullpath(uchar *path)
- {
- register uchar *p=stpcpy(path,basedir);
-
- if (flg_x)
- {
- if (dosfilename[0]=='\\')
- {
- p=path;
- if (*p && p[1]==':')
- {
- if (__mint && toupper(*p)=='U' && p[2]=='\\' && p[3] && p[4]=='\\')
- p+=4;
- else
- p+=2;
- }
- }
-
- strcpy(p,dosfilename);
- }
- else
- strcpy(p,get_fname(dosfilename));
- }
-
- void freshen(void)
- {
- _DOSTIME time;
- register uchar path[MAXPATH];
- register long ok;
- register int cnt=0,attr;
-
- openbackup1();
- message("Freshening archive",arcname);
-
- openbackup2();
- search_lzh(file1,file2,0);
- buffered=0;
-
- while (gethdr(file1,&Hdr1))
- {
- if (!(Hdr1.Attr & FA_DIR) && matchpat(matchfilename,-1,Hdr1.Attr))
- {
- make_fullpath(path);
-
- if (__mint)
- {
- ok=Fxattr(0,path,(uchar *) &xattr);
- time.time=xattr.ctime;
- time.date=xattr.cdate;
- attr=xattr.attr;
- }
- else
- {
- ok=Fsfirst(path,0x07);
- time.time=_dta.dta_time;
- time.date=_dta.dta_date;
- attr=_dta.dta_attribute;
- }
-
- if (!ok && (flg_c || (FTimeToULong(&Hdr1.Ftime)<FTimeToULong(&time))) && (file3=fopen(path,"rb"))!=NULL)
- {
- sethdr(filename,attr,&time,&Hdr2,FAULT);
- freeze(path,(oldtos) ? attr^FA_CHANGED : attr,-1,1);
- close(file3);
- cnt++;
-
- if (flg_d && !(Hdr1.Attr & FA_DIR))
- unlink(path);
-
- sseek(file1,nextarcpos,1);
- continue;
- }
- }
-
- copyold(SUCCS);
- }
-
- if (buffered)
- {
- ShipOut();
- LSeek(file2,arcpos0,SEEK_SET);
- }
-
- endofupdate(cnt);
- }
-
- int Attrib(uchar *p)
- {
- if (__mint)
- {
- if (!Fxattr(0,p,(uchar *) &xattr))
- return(xattr.attr & FA_DIR);
- }
- else if (!Fsfirst(p,-1))
- return(_dta.dta_attribute & FA_DIR);
-
- return(-1);
- }
-
- int tstdir(uchar *name,int flag)
- {
- register uchar path[MAXPATH],*p,yn;
- register int ok;
-
- if (!flag && flg_R && stricmp(get_fname(name),get_fname(matchfilename)))
- {
- printf("%s (%s):\n%s",name,get_fname(filename),M_ENTERNEW);
- gets(path);
-
- if (path[0]!='\0')
- {
- strcpy(backpath(name),get_fname(path));
- Convert_Filename(name,NULL,0);
- }
- }
-
- Again:
- if (flag || flg_x)
- {
- p=name;
- if (*p && p[1]==':')
- {
- p+=2;
-
- if (__mint && toupper(p[-2])=='U' && p[0]=='\\' && p[1] && p[2]=='\\')
- p+=2;
- }
-
- if (*p=='\\')
- p++;
-
- if (flag>=0)
- yn=(flg_m>0 && flg_m<3) ? 'Y' : 'N';
- else
- yn='Y';
-
- while ((p=strchr(p,'\\'))!=NULL)
- {
- memcpy(path,name,p-name);
- path[p-name]=0;
-
- switch(Attrib(path))
- {
- case 0:
- error(MKDIRERR,path,(flag<0) ? SUCCS : FAULT);
- return(FAULT);
- case -1:
- if (yn=='N')
- {
- printf("%s:\n%s",name,M_MKDIR);
-
- if ((yn=get_key("YNA"))=='N')
- return(FAULT);
- else if (yn=='A')
- {
- flg_m=(flg_m) ? 1 : 2;
- yn='Y';
- }
- }
-
- if (Dcreate(path))
- {
- error(MKDIRERR,path,(flag<0) ? SUCCS : FAULT);
- return(FAULT);
- }
- }
-
- p++;
- }
- }
-
- if (!flag)
- {
- if (__mint)
- {
- ok=(int) Fxattr(0,name,&xattr);
- _dta.dta_attribute=xattr.attr;
- _dta.dta_date=xattr.cdate;
- _dta.dta_time=xattr.ctime;
- }
- else
- ok=Fsfirst(name,-1);
-
- if (!ok)
- {
- if ((_dta.dta_attribute & FA_DIR) && !UnixFile)
- {
- if (!flg_q)
- printf("Skipped: %s: Object with same name exists\n",name);
- return(FAULT);
- }
- else if ((!UnixFile || (flg_m & 0x01)) && !flg_c && (((ulong) _dta.dta_date<<16)|(uint) _dta.dta_time)>=FTimeToULong(&Hdr1.Ftime))
- {
- if (!flg_q)
- printf("Skipped: %s: New or same file exists\n",name);
- return(FAULT);
- }
- else if ((flg_m & 0x01)==0)
- {
- if (UnixFile)
- printf("%s (%s):\n%s",name,get_fname(filename),M_OVERWT);
- else
- printf("%s:\n%s",name,M_OVERWT);
-
- if ((yn=get_key("YNRA"))=='R')
- {
- printf(M_ENTERNEW);
- gets(path);
-
- if (path[0]!='\0')
- {
- strcpy(backpath(name),get_fname(path));
- Convert_Filename(name,NULL,0);
- goto Again;
- }
- else
- return(FAULT);
- }
- else if (yn=='N')
- return(FAULT);
- else if (yn=='A')
- flg_m=(flg_m) ? 1 : 3;
- }
-
- if (_dta.dta_attribute & FA_RDONLY)
- {
- if (_dta.dta_attribute!=Hdr1.Attr)
- {
- if (!flg_q)
- printf("%s: %s\n",name,M_RDONLY);
- return(FAULT);
- }
- else
- Fattrib(name,1,_dta.dta_attribute ^ FA_RDONLY);
- }
- }
- }
-
- return(SUCCS);
- }
-
- int tstID(uchar *h)
- {
- static uchar IDpat[7][6]={"lz4","lz5","lh0","lh1","lh5","afx","lhd"};
-
- if (h[0]!='-' || h[4]!='-')
- return(-1);
- else
- {
- register int m=6;
-
- strlwr(++h);
- while (m>=0 && memcmp(h,IDpat[m],3))
- m--;
-
- return((m==5) ? 1 : m);
- }
- }
-
- uchar *Trunc1File(uchar *s,uchar *d)
- {
- register int l=(int) strlen(s);
- register uchar c,*t;
-
- if (fn_name<=12)
- {
- register i,j,k,m;
-
- for (i=l;--i>=0;)
- if (s[i]=='.')
- break;
-
- if (i<0)
- m=l;
- else
- m=i;
-
- t=s;
- for (j=k=0,t=s;j<=7;j++)
- {
- c=*t++;
- if ((i>0 && k++>i) || c=='\0' || j>=m)
- break;
-
- if (c>32 && c!='.' && c!=':' && c!='*' && c!='?')
- *d++=c;
- else
- j--;
- }
-
- if (i>=0 && fn_name==12)
- {
- for (j=4;--j>=0;)
- {
- if ((c=s[i++])=='\0')
- break;
- if (c>32 && c!=':' && c!='*' && c!='?')
- *d++=c;
- }
- }
- }
- else
- {
- strcpy(d,s);
-
- if (l<=fn_name)
- d+=l;
- else if ((t=strrchr(s,'.'))!=NULL && t>s)
- {
- l-=(int) (t-s);
- l=fn_name-l;
- if (d[l-1]=='.')
- l--;
-
- strcpy(d+l,t);
- d+=fn_name;
- }
- else
- d+=fn_name;
- }
-
- *d='\0';
- return(d);
- }
-
- void TruncFile(uchar *s)
- {
- uchar file[MAXPATH],dest[MAXPATH];
- register uchar *f=file,*d=dest,*s1=s,c;
-
- *d='\0';
- while ((c=*s++)!='\0')
- {
- if (c=='\\' || c==':')
- {
- *d='\0';
- f=Trunc1File(dest,f);
- *f++=c;
- *f='\0';
- d=dest;
- }
- else
- *d++=c;
- }
-
- *d='\0';
- f=Trunc1File(dest,f);
- strcpy(s1,file);
- }
-
- int extract(void)
- {
- register uchar *p,*q;
- register int m,cnt=0,succs;
-
- if (cmd=='P')
- {
- if (pager)
- message("Extract from",arcname);
- if (flg_v<2)
- fprintf(file3,"Extract from: %s\n",arcname);
- }
- else
- message("Extract from",arcname);
-
- if (all && exno==0)
- flg_d=0;
-
- if (flg_d)
- {
- flg_z=0;
- openbackup1();
- openbackup2();
- if (search_lzh(file1,file2,1)==FAULT)
- {
- close(file1);
- close(file2);
- unlink(backup2);
- rename(backup1,arcname);
- return(0);
- }
- }
- else if (openarc1(bsize)==FAULT)
- return(0);
-
- infile=file1;
-
- while (gethdr(file1,&Hdr1))
- {
- succs=FAULT;
- if (matchpat(matchfilename,-1,Hdr1.Attr))
- {
- if (cmd=='E')
- make_fullpath(pathname);
-
- if ((m=tstID(Hdr1.HeadID))<0)
- {
- if (!flg_q)
- printf("Skipped: %s: Unknown method\n",filename);
- skipped++;
- }
- else if (cmd=='E' && maxlen>0 && Hdr1.OrgSiz>maxlen)
- {
- if (!flg_q)
- printf("Skipped: %s: File too long\n",filename);
- skipped++;
- }
- else if (cmd=='E' && !flg_a && (Hdr1.Attr & (FA_HIDDEN|FA_SYSTEM)))
- {
- if (!flg_q)
- printf("Skipped: %s: Hidden/System File\n",filename);
- skipped++;
- }
- else if (Hdr1.Attr & FA_DIR)
- {
- if (cmd=='E')
- {
- if (!flg_q)
- {
- if (UnixFile)
- printf("%s (%s): Directory\n",pathname,filename);
- else
- printf("%s: Directory\n",pathname);
-
- if (*comment && flg_x)
- puts(comment);
- }
-
- if ((succs=tstdir(strcat(pathname,"\\"),1))!=0)
- {
- if (!flg_q)
- {
- switch(flg_n)
- {
- case 0:
- printf(" Melted : %c\n",star);
- break;
- case 2:
- puts(" Melted : 100%%");
- break;
- case 3:
- puts(" Melted : -");
- }
- }
-
- if (flg_d)
- message("Deleting",filename);
- }
- }
- else if (!flg_q)
- printf("Skipped: %s: Directory\n",filename);
- }
- else if (cmd!='E' || tstdir(pathname,0))
- {
- textsize=Hdr1.OrgSiz;
- codesize=Hdr1.PacSiz;
-
- cnt++;
- crc=0;
-
- p="Melting ";
- q="Melted ";
-
- switch(cmd)
- {
- case 'E':
- if (!flg_q)
- {
- if (UnixFile)
- printf("%s (%s)\n",pathname,filename);
- else
- puts(pathname);
- }
-
- if ((file3=e_fopen(outfname=pathname,buffer_3,"wb",WTERR,FAULT))==NULL)
- goto _extract_next;
- break;
- case 'T':
- if (!flg_q)
- puts(filename);
- file3=NULL;
- p="Testing ";
- q="Tested ";
- break;
- case 'P':
- if (flg_v<2)
- fprintf(file3,"<<< %s >>>\n\n",filename);
- if (pager && !flg_q)
- puts(filename);
- }
-
- if (!flg_q && *comment && flg_x)
- puts(comment);
-
- if (file3==stdout)
- flg_n=1;
-
- blkdisp(textsize,p,(m==4) ? (N*2) : N);
- outfile=file3;
- succs=SUCCS;
-
- if (m==3)
- Decode();
- else if (m==1)
- DecodeOld();
- else if (m==4)
- succs=decode_lh5(textsize,codesize);
- else
- copyfile(infile,outfile,Hdr1.OrgSiz,1,0);
-
- if (cmd=='E')
- {
- if (fflush(file3))
- error(WTERR,outfname,SUCCS);
-
- if ((!flg_i || flg_i==3) && succs)
- Fdatime(&Hdr1.Ftime,file3->_file,1);
-
- if (fclose(file3))
- error(WTERR,outfname,SUCCS);
-
- if ((!flg_i || flg_i==2) && succs && (Hdr1.Attr!=FA_CHANGED))
- Fattrib(pathname,1,(oldtos) ? (Hdr1.Attr^FA_CHANGED) : Hdr1.Attr);
-
- file3=NULL;
- }
- else if (cmd=='P' && flg_v<2)
- fprintf(file3,"\n");
-
- if (succs)
- {
- if (has_crc && Hdr1.crc!=crc)
- {
- errorlevel|=2;
- succs=FAULT;
- c_err++;
-
- if (!flg_q)
- {
- if (cmd=='P')
- new_line();
- #if GERMAN
- puts("\r Prüfsummen-Fehler ");
- #else
- puts("\r CRC error ");
- #endif
- }
- }
- else if (!flg_q && (cmd!='P' || pager))
- {
- if (flg_n!=1)
- printf("\r %s\n",q);
- if (flg_d)
- message("Deleting",filename);
- }
- }
- }
- }
-
- _extract_next:
- if (succs==FAULT && flg_d)
- copyold(FAULT);
- else if (sseek(file1,nextarcpos,0))
- break;
- }
-
- if (!flg_q && (bad_tab || c_err || skipped || garbage))
- printf("\nBad Tables: %d\nCRC errors: %d\nFiles skipped: %d\nCrashed Files: %d\n",bad_tab,c_err,skipped,garbage);
-
- if (flg_d)
- endofupdate(cnt);
- else
- close(file1);
-
- return(cnt);
- }
-
- void delete(void)
- {
- register int cnt=0;
-
- if (!patno)
- error(NOFNERR,NULL,SUCCS);
-
- openbackup1();
-
- message("Updating archive",arcname);
-
- openbackup2();
- search_lzh(file1,file2,0);
-
- while (gethdr(file1,&Hdr1))
- {
- if (matchpat(matchfilename,-1,Hdr1.Attr))
- {
- message("Deleting",filename);
- cnt++;
- if (sseek(file1,nextarcpos,0))
- break;
- }
- else
- copyold(FAULT);
- }
-
- endofupdate(cnt);
- }
-
- uchar *sysid(void)
- {
- switch(SystemId)
- {
- case 'M':
- return("MSDOS");
- case '2':
- return("OS/2");
- case '9':
- return("OS9");
- case 'K':
- return("OS/68K");
- case '3':
- return("OS/386");
- case 'H':
- return("HUMAN");
- case 'U':
- return("UNIX");
- case 'C':
- return("CP/M");
- case 'm':
- return("Mac");
- case 'R':
- return("Runser");
- case 'A':
- return("Amiga/Atari");
- case 'a':
- return("Atari");
- case 'F':
- return("FLEX");
- case 'X':
- return("XOSK");
- case 'T':
- return("TOWNSOS");
- default:
- return("");
- }
- }
-
- #ifndef __SHELL__
- void list(void)
- {
- static uchar attr[7]="rhs-da";
- register uchar buf[79],*p,*b;
- register ftime *tim=(ftime *) &Hdr1.Ftime;
- register int i,j,k;
- register uint rt;
- ulong Osize,Psize;
- int Fno,Dno;
-
- Osize=Psize=Fno=Dno=0;
-
- #if GERMAN
- printf("Inhalt von: %s\n", arcname);
- #else
- printf("Listing of archive: %s\n", arcname);
- #endif
-
- if (openarc1((drive(arcname)<2) ? 8192L : 1024L)==FAULT)
- return;
-
- if (flg_x<3)
- {
- #if GERMAN
- puts("\n Name Original Gepackt Rate Datum Zeit Attr Typ CRC");
- #else
- puts("\n Name Original Packed Ratio Date Time Attr Type CRC");
- #endif
- puts("-------------- -------- -------- ------ -------- -------- ---- ----- ----");
- }
- else
- puts("\n Name\n--------------");
-
- while (gethdr(file1,&Hdr1))
- {
- if (matchpat(matchfilename,-1,Hdr1.Attr))
- {
- if (flg_x<3)
- {
- rt=ratio(Hdr1.PacSiz,Hdr1.OrgSiz);
- sprintf(buf, " %10lu%10lu %3d.%1d%% %2d-%02d-%02d %2d:%02d:%02d ---w %04X",
- Hdr1.OrgSiz, Hdr1.PacSiz, rt / 10, rt % 10,(tim->year + 80) % 100, tim->mon,
- tim->day, tim->hour, tim->min, tim->sec * 2,Hdr1.crc);
- memcpy(&buf[65],Hdr1.HeadID,5);
-
- for (i=0,j=1;i<6;i++,j<<=1)
- {
- if (Hdr1.Attr & j)
- {
- k=attr[i];
- if (i<=2)
- buf[63-i]=k;
- else
- buf[60]=k;
- }
- }
-
- if (flg_x)
- {
- puts(filename);
- p=sysid();
- b=buf;
-
- while (*p)
- *b++=*p++;
-
- if (flg_x!=2 && *comment)
- puts(comment);
- }
- else
- {
- register uchar *sl=strrchr(filename,'\\');
-
- if ((Hdr1.Attr & FA_DIR) && sl[1]=='\0')
- {
- *sl='\0';
- p=get_fname(filename);
- *sl='\\';
- }
- else
- p=get_fname(filename);
-
- if (p>filename)
- buf[0]='+';
-
- if ((i=(int) strlen(p))>15)
- {
- buf[1]='-';
- i=15;
- }
-
- memcpy(&buf[2],p,i);
- }
-
- puts(buf);
- Osize+=Hdr1.OrgSiz;
- Psize+=Hdr1.PacSiz;
- }
- else
- puts(filename);
-
- if (Hdr1.Attr & FA_DIR)
- Dno++;
- else
- Fno++;
- }
-
- if (sseek(file1,nextarcpos,0))
- break;
- }
-
- if (Fno || Dno)
- {
- if (flg_x<3)
- {
- puts("-------------- -------- -------- ------ -------- --------");
- rt=ratio(Psize, Osize);
- Fdatime(&arcstamp,file1->_file,0);
- tim=(ftime *) &arcstamp;
-
- printf(" %3d files, %10lu%10lu %3d.%1d%% %2d-%02d-%02d %2d:%02d:%02d\n %3d directories\n",
- Fno, Osize, Psize, rt / 10, rt % 10,(tim->year + 80) % 100, tim->mon,
- tim->day, tim->hour, tim->min, tim->sec * 2,Dno);
- }
- else
- {
- puts("--------------");
- #if GERMAN
- printf(" %3d Dateien,\n %3d Ordner\n",Fno,Dno);
- #else
- printf(" %3d files,\n %3d directories\n",Fno,Dno);
- #endif
- }
- }
- else
- #if GERMAN
- puts(" Keine Datei");
- #else
- puts(" no file");
- #endif
- close(file1);
- }
- #endif
-
- void getsw(uchar *p)
- {
- register uchar s,*q;
- register int i;
-
- while ((s=*p++)!='\0')
- {
- q=strchr(swi,s);
- if (q)
- {
- if ((i=(int) (q-swi))<SWI_CNT)
- {
- if (*p=='+')
- {
- *swipos[i]=1;
- p++;
- }
- else if (*p=='-')
- {
- *swipos[i]=0;
- p++;
- }
- else if (*p>='0' && *p<='3')
- *swipos[i]=*p++ - '0';
- else
- *swipos[i]=1;
- }
-
- if (flg_q>1)
- {
- new_line();
- ptitel++;
- flg_q=0;
- }
- if (s=='e' || s=='s')
- {
- if (flg_k<=0)
- {
- flg_k=1;
- ex_len=3;
- }
- }
- else if (s=='r' || s=='X')
- {
- if (!flg_x)
- flg_x++;
- }
- else if (s=='z')
- {
- if (flg_k<=0)
- {
- flg_k=1;
- ex_len=3;
- }
-
- if (*p)
- {
- com_name=p;
- flg_z++;
- }
- break;
- }
- else if (s=='v')
- {
- if (flg_v==3)
- flg_q++;
-
- if (*p)
- {
- if (!flg_v)
- flg_v++;
- pager=p;
- }
- break;
- }
- else if (s=='M')
- {
- if (isdigit(*p))
- maxlen=strtol(p,NULL,10)<<10;
- else
- maxlen=0;
- break;
- }
- else if (s=='N')
- {
- for (i=6;--i>=0;)
- if (!isdigit(*p++))
- break;
-
- if (i<0)
- {
- flg_N++;
- i=(p[-6]-'0')*10 + (p[-5]-'0');
- i|=((p[-4]-'0')*10 + (p[-3]-'0'))<<5;
- i|=((p[-2]-'0')*10 + (p[-1]-'0') - 80)<<9;
- newer.date=i;
- }
- else
- {
- flg_N=0;
- break;
- }
- }
- else if (s=='w')
- {
- if (*p)
- {
- flg_w++;
- strcpy(workdir,p);
- }
- break;
- }
- else if (s=='I')
- {
- if (*p)
- {
- flg_I++;
- flg_x=0;
- strcpy(incldir,p);
- }
- break;
- }
- else if (s=='U')
- {
- if (*p)
- {
- flg_U++;
- unpack=p;
- }
- break;
- }
- else if (s=='P' && *p)
- {
- star=*p++;
- if (*p)
- pnt=*p++;
- break;
- }
- else if (s=='y')
- flg_arc++;
- else if (s=='k')
- {
- if (*p>='0' && *p<='2')
- flg_k=*p++ - '0';
- else
- flg_k=0;
-
- if (!flg_k)
- ex_len=flg_e=flg_z=flg_s=0;
- else
- ex_len=3;
- }
- else if (s=='b')
- flg_backup++;
- else if (s=='5')
- {
- flg_5=1;
- flg_u=0;
- FlgMethod=5;
- }
- else if (s=='4')
- {
- flg_5=0;
- flg_u=flg_4=1;
- }
- else if (s=='l')
- {
- flg_5=flg_u=0;
- FlgMethod=0;
- }
- else if (s=='o')
- {
- flg_5=flg_u=0;
- FlgMethod=1;
- }
- else if (s=='u')
- {
- flg_u=1;
- flg_5=flg_4=0;
- }
- }
- else if (s=='?')
- {
- #ifndef __SHELL__
- if (!ptitel)
- {
- puts(title_x);
- ptitel=1;
- }
-
- puts(use_1);
- puts(use_2);
- puts(use_3);
- #endif
- }
- else
- break;
- }
-
- if (flg_q)
- {
- flg_n=flg_m=1;
- flg_e=flg_z=flg_h=flg_R=0;
- }
- else if (i_handle>0)
- {
- flg_m=1;
- flg_R=0;
- }
- }
-
- int tstsw(uchar *p)
- {
- register uchar s,*q;
- register int i;
-
- while ((s=*p++)!='\0')
- {
- q=strchr(swi,s);
- if (q)
- {
- if (((int) (q-swi))<SWI_CNT)
- if (*p=='+' || *p=='-' || (*p>='0' && *p<='3'))
- p++;
-
- switch (s)
- {
- case 'w':
- case 'z':
- case 'U':
- case 'I':
- case 'v':
- return(SUCCS);
- case 'M':
- return(isdigit(*p));
- case 'N':
- for (i=6;--i>=0;)
- if (!isdigit(*p++))
- return(FAULT);
- break;
- case 'P':
- if (*p)
- p++;
- if (*p)
- p++;
- return((*p!='\0') ? FAULT : SUCCS);
- case 'k':
- if (*p>='0' && *p<='2')
- p++;
- }
- }
- else if (s!='?')
- return(FAULT);
- }
-
- return(SUCCS);
- }
-
- void executecmd(void)
- {
- register int cnt;
-
- INIT_TIMER;
-
- switch(cmd)
- {
- case 'M':
- flg_d=1;
- case 'A':
- flg_c++;
- case 'U':
- flg_chk=0;
- if (cmd=='U')
- flg_A=0;
- append();
- break;
- case 'C':
- flg_X=flg_f=flg_e=flg_z=flg_d=flg_backup=flg_u=flg_4=flg_5=flg_I=0;
- if (flg_k==2)
- flg_k=ex_len=0;
- pack_afx();
- break;
- case 'R':
- case 'F':
- flg_chk=flg_f=0;
- freshen();
- break;
- case 'P':
- if (pager==NULL)
- {
- file3=stdout;
- extract();
- }
- else
- {
- if (flg_w)
- strcpy(pathname,workdir);
- else
- backpath(strcpy(pathname,arcname));
- get_tempname(pathname);
-
- file3=e_fopen(outfname=pathname,buffer_3,"wb",MKTMPERR,SUCCS);
- cnt=extract();
- if (fclose(file3))
- error(WTERR,outfname,SUCCS);
- file3=NULL;
-
- if (cnt)
- {
- uchar buffer[127]="*";
- strncat(buffer,pathname,125);
- Pexec(0,pager,buffer,NULL);
- }
-
- unlink(pathname);
- }
- break;
- case 'X':
- cmd='E';
- case 'T':
- case 'E':
- flg_v=flg_z=0;
- extract();
- break;
- #ifndef __SHELL__
- case 'V':
- if (!flg_x)
- flg_x++;
- case 'L':
- flg_chk=0;
- list();
- break;
- #endif
- case 'D':
- flg_chk=0;
- if (!flg_f)
- flg_f=2;
- delete();
- #ifndef __SHELL__
- break;
- case 'S':
- if (!flg_q)
- puts("Self-Extracting-Files: NOT YET IMPLEMENTED !\7");
- errorlevel|=64;
- lha_exit();
- #endif
- }
-
- EXIT_TIMER;
- if (!flg_q)
- new_line();
- }
-
- void OneNewFile(uchar *p)
- {
- register uchar *s;
- register int len;
-
- if ((s=strpbrk(p,"\n"))!=NULL)
- *s='\0';
-
- if ((len=(int) strlen(p)-1)<0)
- return;
-
- if (!patno && !basedir[0] && !cmdlist && (p[len]=='\\' || p[len]==':'))
- slash(strcpy(basedir,p),1);
- else if (*p!=EXCLUDE && *p!='~')
- {
- if (patno>=MAX_PAT || (fileptr-fileregbuf+strlen(get_fname(p)))>=(FILEBUFSIZ-1))
- message(M_FILETAB,p);
- else
- {
- travel_file[patno]=travel_rel[patno]=0;
-
- while (((s=strstr(p,"\\;"))!=NULL || (s=strstr(p,";\\"))!=NULL))
- {
- if (*s=='\\')
- s++;
- strcpy(s,s+1);
- if (*s=='\\')
- s++;
- travel_rel[patno]=s-p;
- }
-
- strcpy(fileptr,s=get_fname(p));
- *s='\0';
-
- if (*fileptr=='\0')
- {
- travel_wild[patno]=SUCCS;
-
- if (slash(p,-1))
- {
- register uchar path[MAXPATH];
-
- slash(strcpy(path,p),1);
- if ((p=strdup(path))==NULL)
- error(MEMOVRERR,NULL,SUCCS);
- }
- }
- else if (patno>0 && travel_rel[patno]==travel_rel[patno-1] && !strcmp(p,travel_path[patno-1]))
- {
- if (travel_file[patno-1]>0)
- {
- travel_wild[patno-1]|=wildcard(fileptr);
- fileptr[-1]=',';
- while (*fileptr++);
- }
- return;
- }
- else
- {
- travel_file[patno]=(int) (fileptr-fileregbuf);
- travel_wild[patno]=wildcard(fileptr);
- while (*fileptr++);
- }
-
- travel_path[patno]=p;
- travel_len[patno++]=(int) strlen(p);
- }
- }
- else if (*++p!='\0')
- {
- if (exno>=MAX_EXCLD)
- message(M_FILETAB,p);
- else
- exclude_file[exno++]=get_fname(p);
- }
- }
-
- void newfile(uchar *p)
- {
- if (*p=='&' || *p==FILE_LIST)
- {
- register FILE *f;
-
- if (p[1]=='-' && p[2]=='\0')
- f=stdin;
- else
- f=fopen(p+1,"r");
-
- if (f!=NULL)
- {
- register uchar file[MAXPATH],*space;
-
- if (f==stdin)
- puts(M_PATH);
-
- while (fgets(file,MAXPATH-1,f))
- {
- space=file;
- while (*space==' ')
- space++;
- strcpy(file,space);
-
- space=strchr(file,' ');
- if (space)
- *space=0;
-
- Convert_Filename(file,NULL,-1);
- if (file[0]=='&')
- {
- if (strcmp("&-",file))
- newfile(file);
- }
- else if (file[0]!='\r' && file[0]!='\n')
- {
- if ((space=strdup(file))==NULL)
- error(MEMOVRERR,NULL,SUCCS);
- OneNewFile(space);
- }
- else if (f==stdin)
- break;
- }
-
- if (f!=stdin)
- close(f);
- }
- }
- else
- OneNewFile(p);
- }
-
- void copyfile(FILE *Source,FILE *Dest,long size,int crcflg,int bufflg)
- {
- register long n,block;
-
- if (bufflg)
- Dest=NULL;
-
- if (crcflg)
- crc=0;
-
- if (!crcflg || blocksize>bsize || flg_n==1)
- block=bsize;
- else
- block=blocksize;
-
- while (size>0)
- {
- n=block>size ? size : block;
- if (fread((bufflg) ? outrec.ptr : buffer_gen,n,1,Source)!=1)
- error(RDERR,infname,SUCCS);
-
- if (crcflg)
- {
- if (!flg_chk)
- block_crc(n,(bufflg) ? outrec.ptr : buffer_gen);
- ProcInd();
- }
-
- if (Dest && fwrite(buffer_gen,n,1,Dest)!=1)
- error(WTERR,outfname,SUCCS);
-
- if (bufflg)
- {
- outrec.ptr+=n;
- outrec.cnt-=n;
- }
-
- size-=n;
- }
-
- if (ferror(Source))
- error(RDERR,infname,SUCCS);
- }
-
- void EncodeOld(void)
- {
- register uchar code_buf[34],*code=code_buf,*ptr,*tbuf=text_buf;
- register int i,r,s=0,m=N-1,c,last_match_length;
- ulong printcount;
- uchar mask=1;
- int len;
-
- printcount=textsize=0;
- InitTree();
-
- *code++=0;
- for (i=r=(N-F),ptr=tbuf;--i>=0;)
- *ptr++=' ';
-
- for (i=0;i<F && (c=crc_getc(infile))!=EOF;i++)
- *ptr++=c;
-
- textsize=(len=i);
- if (!textsize)
- return;
-
- for (i=1;i<=F;i++)
- InsertONode(r - i);
- InsertONode(r);
-
- do
- {
- if (match_length>len)
- match_length=len;
-
- if (match_length <=THRESHOLD)
- {
- match_length=1;
- code_buf[0]|=mask;
- *code++=tbuf[r];
- }
- else
- {
- *code++=(uchar) match_position;
- *code++=(uchar) (((match_position >> 4) & 0xf0) | (match_length - (THRESHOLD + 1)));
- }
-
- if (!(mask<<=1))
- {
- i=(int) (code - code_buf);
- ptr=(code=code_buf);
-
- while (--i>=0)
- buf_putc(*ptr++);
-
- *code++=0;
- mask=1;
- }
-
- last_match_length=match_length;
- for (i=0;i<last_match_length && (c=crc_getc(infile))!=EOF;i++)
- {
- DeleteONode(s);
- tbuf[s]=c;
- if (s < F - 1)
- tbuf[s + N]=c;
- s=(++s) & m;
- r=(++r) & m;
- InsertONode(r);
- }
-
- if ((textsize+=i)>printcount)
- {
- ProcInd();
- printcount+=blocksize;
- }
-
- while (i<last_match_length)
- {
- DeleteONode(s);
- s=(++s) & m;
- r=(++r) & m;
- if (--len)
- InsertONode(r);
- i++;
- }
- } while (len>0);
-
- if ((i=(int) (code - code_buf))>1)
- {
- ptr=code_buf;
- while (--i>=0)
- buf_putc(*ptr++);
- }
-
- if (!buffered)
- shipout();
- }
-
- void DecodeOld(void)
- {
- register int i,k,r=N-F,c=0x2020,m=N-1,*tbuf=(int *) text_buf;
- register uint crcr=crc;
- long todo=codesize,done=blocksize;
- uint flags=0;
-
- if (outfile)
- OpenOut();
- for (k=(N-F)>>1;--k>=0;)
- *tbuf++=c;
-
- for (;;)
- {
- if (!((flags>>=1) & 256))
- {
- c=Bgetc(infile);
- if (todo--==0)
- break;
- flags=c|0xff00;
- }
-
- if (todo--==0)
- break;
-
- if (flags & 1)
- {
- c=Bgetc(infile);
- if (outfile)
- buf_putc(c);
- set_crc(crcr,c);
-
- text_buf[r++]=c;
- r&=m;
-
- if (done--==0)
- {
- ProcInd();
- done=blocksize;
- }
- }
- else
- {
- i=Bgetc(infile);
- if (todo--==0)
- break;
-
- i|=(((k=Bgetc(infile)) & 0xf0)<<4);
- k=(k & 0x0f) + (THRESHOLD+1);
-
- for (;--k>=0;i++)
- {
- c=text_buf[i & m];
- if (outfile)
- buf_putc(c);
- set_crc(crcr,c);
-
- text_buf[r++]=c;
- r&=m;
-
- if (done--==0)
- {
- ProcInd();
- done=blocksize;
- }
- }
- }
- }
-
- if (outfile)
- shipout();
- ProcInd();
- crc=crcr;
- }
-
- int fread_crc(uchar *p,int n,FILE *f)
- {
- n=(int) fread(p,1,n,f);
- if (ferror(f))
- error(RDERR,infname,SUCCS);
- block_crc(n,p);
- return(n);
- }
-
- void count_t_freq(void)
- {
- register int i,k,n;
- register ushort *tfreq,tf0,tf1;
- register uchar *clen;
-
- for (i=NT,tfreq=t_freq,k=0;--i>=0;)
- *tfreq++=k;
-
- clen=&c_len[n=NC];
- while (*--clen==0 && --n>=0);
-
- i=tf0=tf1=0;
- clen=c_len;
- tfreq=t_freq;
-
- while (i<n)
- {
- i++;
- if ((k=*clen++)==0)
- {
- k=1;
- while (i<n && *clen==0)
- {
- clen++;
- k++;
- i++;
- }
-
- if (k<=2)
- tf0+=k;
- else if (k<=18)
- tf1++;
- else if (k==19)
- {
- tf0++;
- tf1++;
- }
- else
- tfreq[2]++;
- }
- else
- tfreq[k+2]++;
- }
-
- tfreq[0]+=tf0;
- tfreq[1]+=tf1;
- }
-
- void write_pt_len(register int n,int nbit,register int i_special)
- {
- register uchar *ptlen;
- register int i,k,c=3;
-
- ptlen=&pt_len[n];
- while (*--ptlen==0 && --n>=0);
-
- putbits(nbit,n);
-
- i=0;
- ptlen=pt_len;
- while (i<n)
- {
- i++;
- if ((k=*ptlen++)<=6)
- putbits(3,k);
- else
- {
- k-=c;
- putbits(k,(1U<<k)-2);
- }
-
- if (i==i_special)
- {
- while (i<6 && *ptlen==0)
- {
- ptlen++;
- i++;
- }
-
- putbits(2,(i-c) & c);
- }
- }
- }
-
- void write_c_len(void)
- {
- register int i,k,n;
- register uchar l0=pt_len[0],*clen;
- register ushort c0=pt_code[0],c1=pt_code[1];
-
- clen=&c_len[n=NC];
- while (*--clen==0 && --n>=0);
-
- putbits(CBIT,n);
-
- i=0;
- clen=c_len;
- while (i<n)
- {
- i++;
- if ((k=*clen++)==0)
- {
- k=1;
- while (i<n && *clen==0)
- {
- clen++;
- k++;
- i++;
- }
-
- switch (k)
- {
- case 2:
- putbits(l0,c0);
- case 1:
- putbits(l0,c0);
- break;
- case 19:
- putbits(l0,c0);
- putbits(pt_len[1],c1);
- putbits(4,15);
- break;
- default:
- if (k<=18)
- {
- putbits(pt_len[1],c1);
- putbits(4,k-3);
- }
- else
- {
- putbits(pt_len[2],pt_code[2]);
- putbits(CBIT,k-20);
- }
- }
- }
- else
- putbits(pt_len[k+2],pt_code[k+2]);
- }
- }
-
- void start_huf(void)
- {
- register ushort *p,v=0;
- register int i;
-
- text_buf[0]=v;
- for (i=NC,p=c_freq;--i>=0;)
- *p++=v;
-
- for (i=NP,p=p_freq; --i>=0;)
- *p++=v;
-
- subbitbuf=v;
- bitcount=CHAR_BIT;
- }
-
- void end_huf(void)
- {
- send_block();
- putbits(CHAR_BIT-1,0);
- }
-
- int read_pt_len(int nn,int nbit,int i_special)
- {
- register uchar *ptlen=pt_len;
- register int n;
-
- if ((n=getbits(nbit))==0)
- {
- register ushort *table=pt_table,c;
-
- for (n=nn,c=0;--n>=0;)
- *ptlen++=c;
- for (n=256,c=getbits(nbit);--n>=0;)
- *table++=c;
- }
- else
- {
- register int c,i=0;
- register uint mask,bibu;
-
- while (i<n)
- {
- bibu=bitbuf;
- if ((c=bibu>>13)==7)
- {
- mask=1U<<12;
- while (mask&bibu)
- {
- mask>>=1;
- c++;
- }
- }
-
- fillbuf((c<7) ? 3 : c-3);
- *ptlen++=c;
-
- i++;
- if (i==i_special)
- {
- i+=(c=getbits(2));
- mask=0;
- while (--c>=0)
- *ptlen++=mask;
- }
- }
-
- c=0;
- n=nn;
- while (i<n)
- {
- *ptlen++=c;
- i++;
- }
-
- if (!make_table(n,pt_len,8,pt_table))
- return(FAULT);
- }
-
- return(SUCCS);
- }
-
- int read_c_len(void)
- {
- register uchar *clen=c_len;
- register int n,i;
-
- if ((n=getbits(CBIT))==0)
- {
- register ulong *daddy=(ulong *) dad,k;
-
- for (i=(NC>>1),k=0;--i>=0;*((int *) clen)++=(int) k);
- k=getbits(CBIT);
- k=(k<<16)|k;
- for (i=2048;--i>=0;*daddy++=k);
- }
- else
- {
- register ushort *pttable=pt_table;
- register uint mask,bibu;
- register int c;
-
- i=0;
- while (i<n)
- {
- bibu=bitbuf;
- c=pttable[bibu>>8];
- if (c>=NT)
- {
- mask=1U<<7;
- do
- {
- if (bibu&mask)
- c=right[c];
- else
- c=left[c];
- mask>>=1;
- } while (c>=NT);
- }
-
- fillbuf(pt_len[c]);
- if (c>2)
- {
- *clen++=(c-2);
- i++;
- }
- else if (c)
- {
- if (--c==0)
- c=getbits(4)+3;
- else
- c=getbits(CBIT)+20;
- i+=c;
- mask=0;
- while (--c>=0)
- *clen++=mask;
- }
- else
- {
- *clen++=c;
- i++;
- }
- }
-
- mask=0;
- bibu=NC;
- while (i<bibu)
- {
- *clen++=mask;
- i++;
- }
-
- if (!make_table(NC,c_len,12,(ushort *) dad))
- return(FAULT);
- }
-
- return(SUCCS);
- }
-
- void make_len(int root)
- {
- register int i,k;
- register uint cum=0;
- register ushort *lencnt=len_cnt,*spt=sortptr;
-
- for (i=17;--i>=0;)
- *lencnt++=cum;
-
- count_len(root);
- for (i=17,k=0,lencnt=&len_cnt[17];--i>=0;)
- cum+=(*--lencnt)<<(k++);
-
- while (cum!=(1U<<16))
- {
- len_cnt[16]--;
-
- for (i=17,lencnt=&len_cnt[17];--i>=0;)
- if (*--lencnt)
- {
- lencnt[0]--;
- lencnt[1]+=2;
- break;
- }
- cum--;
- }
-
- for (i=17,lencnt=&len_cnt[17];--i>=0;)
- {
- k=*--lencnt;
- while (--k>=0)
- len[*spt++]=i;
- }
-
- sortptr=spt;
- }
-
- #ifdef __SHELL__
- #define print_title(a)
- #else
- void print_title(uchar **argv)
- {
- if (!flg_q)
- {
- #if BETA==0
- register uchar *env;
- #endif
-
- if (!ptitel)
- {
- puts(title);
- ptitel++;
- }
-
- #if BETA
- if (!pargs)
- #else
- if (!pargs && (env=getenv("LHARCPAR"))!=NULL && atoi(env))
- #endif
- {
- register int i;
-
- pargs++;
- #if GERMAN
- puts("Argumente:");
- #else
- puts("Arguments:");
- #endif
- for (i=1;i<args;i++)
- printf("'%s'\n",argv[i]);
- new_line();
- }
- }
- }
- #endif
-
- uchar *get_ext(void)
- {
- if (case_sensitive(arcname)==_PC_CASECONV)
- return(".LZH");
- else
- return(".lzh");
- }
-
- uchar *device(uchar *name)
- {
- if (name[3]==':')
- name[3]='\0';
-
- if (!stricmp(name,"PRN") || !stricmp(name,"PRT"))
- return("PRN:");
- else if (!stricmp(name,"AUX"))
- return("AUX:");
- else if (!stricmp(name,"CON"))
- return("CON:");
- else
- {
- Convert_Filename(name,NULL,-1);
- return(name);
- }
- }
-
- int dev_arc(void)
- {
- register uchar buf[MAXPATH],*path;
-
- if (__mint)
- {
- if (get_fname(arcname)==arcname)
- path=act_dir;
- else
- path=arcname;
-
- if (path[0]=='\0' || path[1]!=':')
- {
- buf[0]=Dgetdrv()+'a';
- buf[1]=':';
- if (path[0]!='\\')
- {
- buf[2]='\\';
- strcpy(buf+3,path);
- }
- else
- strcpy(buf+2,path);
- }
- else
- strcpy(buf,path);
-
- strupr(buf);
-
- if (buf[0]=='U')
- {
- if (!strncmp(buf+3,"DEV\\",4))
- return(1);
- else if (!strncmp(buf+3,"SHM\\",4) || !strncmp(buf+3,"PROC\\",5) || !strncmp(buf+3,"PIPE\\",5))
- return(-1);
- }
- }
-
- return(0);
- }
-
- #ifndef __SHELL__
- void ioredirect(int argc,uchar **argv)
- {
- register uchar *p,c;
- register int i;
-
- for (i=0;i<argc;i++)
- {
- p=*argv++;
-
- if ((c=*p++)=='>' && o_handle<0)
- {
- if (!args)
- args=i;
-
- if (*p=='>')
- {
- if ((o_handle=Fopen(o_dir=device(++p),1))>=0)
- {
- o_dev=Fdup(1);
- Fseek(0l,(int) o_handle,SEEK_END);
- Fforce(1,(int) o_handle);
- continue;
- }
- }
-
- if ((o_handle=Fcreate(o_dir=device(p),0))>=0)
- {
- o_dev=Fdup(1);
- Fforce(1,(int) o_handle);
- }
- }
- else if (c=='<' && i_handle<0)
- {
- if (!args)
- args=i;
-
- if ((i_handle=Fopen(p,0))>=0)
- {
- i_dev=Fdup(0);
- Fforce(0,(int) i_handle);
- flg_m=1;
- }
- }
- }
- }
- #endif
-
- #ifndef __SHELL__
- void main(int argc,uchar **argv)
- {
- register uchar *p,*q,*env,*env9,**old_argv=argv;
-
- ioredirect(argc,argv);
- if (args)
- argc=args;
- else
- args=argc;
- #else
- void argvmain(int argc,uchar **argv)
- {
- register uchar *p,*q,*env,*env9;
-
- args=argc;
- #endif
-
- if ((env=getenv("COLUMNS"))!=NULL)
- {
- maxblk=atoi(env) - 16;
- if (maxblk<24)
- maxblk=24;
- }
-
- argc--;
- #ifndef __SHELL__
- if (argc<=0)
- {
- flg_h=1;
- puts(title_x);
- puts(use_1);
- wait_for_key(0);
- putchar('\r');
- puts(use_2);
- wait_for_key(0);
- putchar('\r');
- puts(use_3);
- }
- else
- #endif
- {
- _lseed=clock();
- mkcrc();
- #if __030
- get_cpu();
- #endif
-
- Fsetdta(&_dta);
- getcwd(act_dir,MAXPATH);
- unix2dos(act_dir,0);
- slash(act_dir,1);
- Convert_Filename(act_dir,NULL,-1);
-
- fileptr=fileregbuf;
- *fileptr++='*';
- *fileptr++='\0';
-
- {
- register uint vers=Sversion();
- vers=(vers<<8) | (vers>>8);
-
- if (__mint)
- oldtos=0;
- else
- oldtos=(vers<0x0014);
- }
-
- buffer_gen=(uchar *) (((long) (buffer+128)) & (~15l));
-
- argv++;
- argc--;
- cmd=toupper(*(p=*argv++));
-
- #ifndef __SHELL__
- if (p[1]!='\0' || strchr("EXTDLVAUMFPRSC",cmd)==NULL || !argc)
- {
- register uchar dir[MAXPATH];
- register int compr;
-
- Convert_Filename(p,NULL,-1);
- if ((q=strstr(p,"*.*"))>NULL)
- {
- if (q==p)
- p=strcpy(dir,act_dir);
- else
- *q='\0';
- }
- slash(p,0);
-
- if (Attrib(p)>0)
- compr=1;
- else
- {
- switch (test_afx(p))
- {
- case 3:
- {
- register uchar *b=buffer_gen;
-
- q=b+1536;
- compr=1;
-
- while (b<q)
- {
- if (!strncmp(b,"SFX",3))
- goto _extract;
- else if (*b++=='-' && b[3]=='-' && (*b=='a' || *b=='A' || *b=='l' || *b=='L'))
- break;
- }
- }
- break;
- case 2:
- _extract:
- compr=0;
- break;
- default:
- compr=1;
- }
- }
-
- if (strpbrk(get_fname(p),"*?")==NULL && compr)
- {
- cmd='U';
- pack++;
-
- strcpy(arcname,p);
- p=get_fname(p);
- if ((q=strrchr(p,'.'))!=NULL)
- *q='\0';
-
- strcpy(stpcpy(backpath(arcname),p),get_ext());
-
- if (q)
- *q='.';
- }
- else
- {
- cmd='X';
- flg_g++;
- backpath(strcpy(basedir,p));
- pack+=2;
- }
-
- flg_x=3;
- flg_m++;
- argc++;
- argv--;
- }
- #endif
-
- cmdupdate=strchr("AUMFRD",cmd)!=NULL;
- cmdlist=strchr("AUMC",cmd)!=NULL;
-
- if ((env=getenv("TMP"))!=NULL || (env=getenv("TMPDIR"))!=NULL)
- {
- flg_w++;
- strcpy(workdir,env);
- }
-
- if ((env=getenv("UNPACKED"))!=NULL)
- {
- flg_U++;
- unpack=env;
- }
-
- if ((env=getenv("LHARC"))!=NULL)
- {
- for (p=env;*p;p++)
- if (*p==' ' || *p=='\x08')
- *p=0;
- env9=p;
- p=env;
- while (p<env9)
- {
- while (!*p)
- p++;
- if (*p=='-' || *p=='/')
- p++;
- getsw(p);
- while (*p)
- p++;
- }
- }
-
- if (cmd=='C' || arcname[0])
- patno=0;
- else
- patno=-1;
-
- while (argc--)
- {
- p=*argv++;
- if (*p=='-')
- {
- getsw(++p);
- continue;
- }
- else if (*p=='/' && patno<0 && tstsw(p+1))
- {
- if (cmdupdate && (wildcard(get_fname(p+1)) || multi_wild(p+1)))
- {
- getsw(++p);
- continue;
- }
- else if ((q=strrchr(get_fname(p),'.'))!=NULL)
- {
- if (arc_ext(q)==FAULT)
- {
- getsw(++p);
- continue;
- }
- }
- else
- {
- getsw(++p);
- continue;
- }
- }
-
- print_title(old_argv);
- Convert_Filename(p,NULL,-1);
-
- if (patno<0)
- {
- p=get_fname(strcpy(arcname,p));
-
- if (*p=='\0' || (Device=dev_arc())<0)
- error(NOARCNMERR,NULL,SUCCS);
-
- multi_arc=multi_wild(p);
- wild_arc=wildcard(p)|multi_arc;
-
- if (cmdupdate && (strpbrk(p,"*?") || multi_arc))
- error(NOARCNMERR,arcname,SUCCS);
-
- if (!multi_arc && !Device)
- {
- if ((q=strrchr(p,'.'))==NULL)
- strcat(arcname,get_ext());
- else if (q[1]=='\0')
- strcpy(q,get_ext());
- else if (flg_m!=1 && cmdupdate && arc_ext(q)==FAULT)
- {
- printf(M_NOTLZH,get_fname(arcname));
- if (get_key("YN")=='N')
- {
- errorlevel|=128;
- lha_exit();
- }
- }
- }
- patno++;
- }
- else if (pack<2)
- {
- if (pack)
- {
- slash(p,0);
-
- if (strstr(p,"*.*")!=NULL)
- flg_r++;
- else if (Attrib(p)>0)
- {
- register uchar path[MAXPATH];
-
- flg_r++;
- slash(strcpy(path,p),1);
- if ((p=strdup(path))==NULL)
- error(MEMOVRERR,NULL,SUCCS);
- }
- }
-
- newfile(p);
- }
- }
-
- if (args>2 && pack==1)
- pack=0;
- else if (pack==2)
- pack=0;
-
- print_title(old_argv);
-
- if (patno<0)
- error(NOARCNMERR,NULL,SUCCS);
- else if (!patno && cmd!='D')
- {
- travel_path[0]="";
- travel_len[0]=travel_file[0]=travel_rel[0]=0;
- travel_wild[0]=all=SUCCS;
- patno++;
- flg_p=0;
- }
-
- fn_name=path_conf(basedir,_PC_NAME_MAX);
- pt_name=path_conf(basedir,_PC_PATH_MAX);
- if (pt_name>MAXPATH)
- pt_name=MAXPATH;
- Case=case_sensitive(basedir);
-
- if (flg_U)
- Convert_Filename(unpack,NULL,0);
-
- Convert_Filename(workdir,NULL,0);
- slash(workdir,1);
- tstdir(workdir,-1);
-
- Convert_Filename(incldir,NULL,0);
- slash(incldir,1);
-
- if (Device)
- {
- if (strchr("AMEXTPLV",cmd)==NULL)
- error(NOARCERR,arcname,SUCCS);
- else if (strchr("EXTP",cmd))
- flg_d=0;
- flg_w=0;
- }
-
- MakeBuffers();
-
- if (cmdupdate || cmd=='C')
- executecmd();
- else
- {
- register uchar *f,*p,*q,arc[MAXPATH];
- register long handle,buf[MAXPATH>>2];
- register int done,cnt=0,len,case_s;
-
- if (__mint)
- _gemdos=(path_conf(arcname,_PC_NAME_MAX)<=12 && case_sensitive(arcname)==_PC_CASECONV) ? 1 : 0;
- else
- _gemdos=1;
-
- if (!_gemdos)
- {
- backpath(strcpy(arc,arcname));
-
- if (arc[0]!='\0')
- handle=Dopendir(arc,0);
- else
- handle=Dopendir(act_dir,0);
-
- if ((handle&0xff000000l)==0xff000000l)
- error(NOARCERR,arcname,SUCCS);
- else
- done=(int) Dreaddir(MAXPATH,handle,(uchar *) buf);
- f=(uchar *) &buf[1];
- }
- else
- {
- strcpy(arc,arcname);
- if (wild_arc)
- strcpy(backpath(arc),"*.*");
- done=Fsfirst(arc,0x07);
- f=_dta.dta_name;
- }
-
- case_s=(flg_S==2 || (!flg_S && case_sensitive(arcname)!=_PC_CASESENS));
- q=get_fname(arcname);
- p=backpath(arc);
-
- while(!done)
- {
- if (case_s)
- strupr(f);
-
- if ((_gemdos && wild_arc==FAULT) || chk_wild(f,q,flg_W))
- {
- strcpy(p,f);
- if (o_dir!=NULL && !strcmp(p,o_dir))
- goto arc_next;
-
- if (!_gemdos)
- {
- if (*f!='.' || (strcmp(f,".") && strcmp(f,"..")))
- {
- Fxattr(0,arc,(uchar *) &xattr);
- if (xattr.attr & (FA_LABEL|FA_DIR))
- goto arc_next;
- *p='\0';
- }
- else
- goto arc_next;
- }
-
- if (cnt<MAX_ARC)
- {
- len=(int) strlen(f)+1;
- if ((fileptr-fileregbuf+len)>=(FILEBUFSIZ-1))
- message(M_FILETAB,f);
- else
- {
- strcpy(fileptr,f);
- arc_file[cnt++]=(int) (fileptr-fileregbuf);
- fileptr+=len;
- }
- }
- else
- message(M_FILETAB,f);
- }
-
- arc_next:
- if (_gemdos)
- done=Fsnext();
- else
- done=(int) Dreaddir(MAXPATH,handle,(uchar *) buf);
- }
-
- if (!_gemdos)
- Dclosedir(handle);
-
- if (!cnt)
- error(NOARCERR,arcname,SUCCS);
- else
- {
- if (!flg_q)
- #if GERMAN
- printf(" Archive gefunden : %d\n",cnt);
- #else
- printf(" Archives matched : %d\n",cnt);
- #endif
-
- q=basedir+strlen(basedir);
-
- while(--cnt>=0)
- {
- f=&fileregbuf[arc_file[cnt]];
- strcpy(backpath(arcname),f);
- if (cmd=='X' || cmd=='E')
- {
- if (flg_g && (p=strrchr(f,'.'))!=NULL)
- {
- *p='\0';
- strcpy(stpcpy(q,f),"\\");
- }
-
- tstdir(basedir,-1);
- }
-
- executecmd();
- }
- }
-
- if (cmd!='L' && cmd!='V')
- tstpat();
- }
- }
-
- lha_exit();
- }
-